how to get rid of blank line in a flat text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get rid of blank line in a flat text file
# 1  
Old 05-22-2003
how to get rid of blank line in a flat text file

Hi, I have a flat text file which contains blank line between each text line. Is there any command to get rid of it?

Thanks for your help
# 2  
Old 05-22-2003
You can use awk:
Code:
awk '$1 !~ /(^$)/' someFile > someNewFile
mv someNewFile someFile

Or you can use sed:
Code:
sed '/^$/ d' someFile > someNewFile
mv someNewFile someFile

And here's a script copied from the man page for sed:
Code:
In this example, sed removes all blank lines (including those with just
<Tab> and <Space> characters) from padded_file:

   sed '
   /^$/ d
   /^[<Tab><Space>]*$/ d
   ' padded_file


Last edited by oombera; 05-22-2003 at 11:55 AM..
# 3  
Old 05-22-2003
Re: how to get rid of blank line in a flat text file

Quote:
Originally posted by xfang
Hi, I have a flat text file which contains blank line between each text line. Is there any command to get rid of it?

Thanks for your help
Thanks a lot oombera. I tested it out, it works fine. I have another question, in the text file, if the first character in each line starts with "E", then get rid of the whole line.

Thanks

Thanks, I got the result I want. But I didn't quite understand what ^$ means.

Last edited by xfang; 05-22-2003 at 04:33 PM..
# 4  
Old 05-22-2003
You can use either of the first two scripts I posted .. just replace ^$ with ^E
# 5  
Old 05-22-2003
To reply to the edit you made in your post, when using commands like sed and awk that search for patterns, there are special characters, such as ^ (which represents the beginning of a line) and $ (which represents the end of a line).

Basically, whereas ^E searches for any line with an E immediately following the beginning of the line, ^$ searches for any line whose end immediately follows its beginning (a blank line).
# 6  
Old 05-22-2003
Re: Re: how to get rid of blank line in a flat text file

Quote:
Originally posted by xfang
Thanks a lot oombera. I tested it out, it works fine. I have another question, in the text file, if the first character in each line starts with "E", then get rid of the whole line.

Thanks

Thanks, I got the result I want. But I didn't quite understand what ^$ means.
Thanks oombera, I learned a lot from you today.
# 7  
Old 05-26-2003
You can use grep command also for the same
grep -v "^$" someFile > someNewFile

Regards,
Yeheya
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

How to identify exact text and then add a blank line above it using sed?

I need to identify the exact text of San Antonio Generator Running in the output my script which lands to a text file. Once SED finds the specific text, I need it to insert one line above the matched text. Here is what I have so far that isn't working all that well for me. Any help would be... (7 Replies)
Discussion started by: jbrass
7 Replies

3. Shell Programming and Scripting

Get line number in flat file

Hi, Is there a way to find out the line number from where the data starts? like if the data contains column header, irrespective of the text in the column header we should get the line number from which contains the column header. I am sorry if I haven't explained the problem clearly. ... (8 Replies)
Discussion started by: kedar_laveti
8 Replies

4. Shell Programming and Scripting

How to remove blank line from a text file?

Hi All, I am creating a text file using perl. The first record I am writing as "$line" and all the other as "\n$line". At the end the file is having N number of lines. I am using this file for MLOAD (Teradata), which is reading N+1 lines in the file and failing.I am not able to find new line... (2 Replies)
Discussion started by: unankix
2 Replies

5. UNIX for Dummies Questions & Answers

Any way to get rid of ^M characters in a text file using pr?

When I use vi to see what's in the file I get this: int add1(int x) {^M return x + 1;^M} ^Mint subtract1(int x) {^M return x - 1;^M} ^Mint double_it(int x) {^M return x * 2;^M} ^Mint halve_it(int x) {^Mreturn x / 2;^M} ^Mint main() {^M int myint;^M int result;^M ... (2 Replies)
Discussion started by: Nonito84
2 Replies

6. Shell Programming and Scripting

Delete the last empty/blank line of the text file

Hi All, I have a file.txt which seems like having three lines. wc -l file.txt 3 file.txt In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they... (6 Replies)
Discussion started by: senayasma
6 Replies

7. Shell Programming and Scripting

Getting Rid of Having to Write to Flat Files

Ok, so i've been having to write to flat files lately and then making my script read information from the flat file and then work off of that. i dont want to keep doing that because i believe it creates a mess. i like to keep my work all to one script instead of having that one script... (7 Replies)
Discussion started by: SkySmart
7 Replies

8. Shell Programming and Scripting

Random word from a flat text file

Hello, I need to take a random word from a flat text file with words in it seperated by spaces. The code I am using, always gives me the first word. Can anyone please shed some light on this. Here's my code. Thanks echo table roof ceiling jar computer monitor keyboard carpet >... (5 Replies)
Discussion started by: Freakhan
5 Replies

9. Shell Programming and Scripting

insert text and add blank line

I need your help, I want to add a text every 2nd line and also a blank line after 3 line (In the output 2nd line is "changetype: modify" and every 4th line is blank line) Input file format dn: abc orclsourceobjectdn: abcd dn: bcd orclsourceobjectdn: bcda dn: cba orclsourceobjectdn:... (7 Replies)
Discussion started by: athidhi
7 Replies

10. UNIX for Dummies Questions & Answers

Creating flat text file (ASCII)

Hi everybody. I need help and I hope someone is willing to help me out here. My wholesale company is currently moving to new software. The old software is running on a UNIX platform. We need to migrate data from the UNIX system, but our former software provider refuses to assist the data... (5 Replies)
Discussion started by: Wdonero
5 Replies
Login or Register to Ask a Question