Tuesday, January 29, 2013

Deleting empty lines from a file

Some times there are some empty lines which we feel are redundant in the file and want to remove them.Below is the command in unix to do that.

sed -i 's/^$//g' your_file

But there is also another way to do this:

grep . your_file > dest_file

In perl also we can acheive this as below:

perl -pi -e 's/^$//g' your_file

the above mentioned perl and sed solutions will do an inplace replacement in the file

If in case the lines have some spaces then:

perl -pi -e 's/^\s*$//g' your_file

No comments:

Post a Comment