DelphiFAQ Home Search:

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

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and 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

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

 

 

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.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

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.