Tuesday, June 20, 2006

Perl : Removing blank lines from a file

You can do intricate things even on command line. The following command prints filename on the command standard output after removing blank lines

cat filename | perl -ne 's/^\s*\n$//; print;'

Notes
  • Replace filename with the path of the file from which you want to remove blank lines.
  • You can optionally redirect the output by using '>' operator to create a new file.

1 comment:

Ketan said...

Better way of doing the same thing :-)

cat filename | perl -ne 'next if /^\s*$/; print;'