DelphiFAQ Home Search:

How to remove blank lines from a text file (in Unix)

 

comments5 comments. Current rating: 5 stars (2 votes). Leave comments and/ or rate it.

Question:

I need to remove all blank lines from a huge text file on a unix system. What's the easiest way to achieve this?

Answer:

Use the command line text editor sed and issue the statement shown below.
It will create a temporary file, afterwards replace the original file with it.

# the following uses the editor sed to find blank lines
# and delete them:

# in the regex ^ means beginning of line, 
# and $ means end of line

sed '/^$/d' myfile.txt >newfile.txt
mv newfile.txt myfile.txt

Content-type: text/html

Comments:

2007-10-03, 03:48:29
anonymous from United States  
thanks a lot...
sed '/^$/d' myfile.txt >newfile.txt
is the only command worked for me after nearly an hour of using various scripts and techniques.

thanks a lot again
sundar.ece@gmail.com
2008-11-26, 07:44:44
prashantdeshani@yahoo.com from Switzerland  
Hi,

I have tried the sed command as given in the example above.

It works fine except a small strange behaviour:

It 'always' removes the last line of the file.

Do let me know if I need to add something in the command line as I am doing exactly what is written above.

Thanks and Best Regards,
Prashant Deshani
2008-12-26, 05:49:40
[hidden] from United Kingdom  
rating
Thanks for your advice about removing blank line from a text file (in Unix).
Could you please also advise, how to split up a 'one line' document into a multi-line document. For example:-
a) Origindocument: -
<card><firstname>john</firstname&ge><lastname>smith</lastname></card>
b) RequirDocument
<card>
<firstname>john</firstname>
<lastname>smith</lastname>
</card>


I should be grateful for any advice.

John Smith
=========
2009-04-06, 06:05:35
anonymous from India  
thanks a lot for this command
2012-04-04, 04:41:27
anonymous from India  
rating
Thanks a lot .

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: