Delete empty lines in a file

Tuesday, January 29, 2013 , , 1 Comments

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 '/^$/d' 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

1 comment:

  1. nice... but take note the '-i' in sed is not posix compliant!?

    ReplyDelete