3 ways to remove blank lines from a file

There are certainly more than 3 ways to do this. Typically I’ve always used sed to do this, but here’s my method using sed and two other methods using tr and awk:

sed:

sed '/^$/d' file_with_blank_lines

tr:

tr -s '\n' <file_with_blank_lines

awk:

awk '{ if ($0) print $0 }' file_with_blank_lines

If you have other favorite ways, leave a note in the comments!

One thought on “3 ways to remove blank lines from a file”

  1. If you don’t care about the order of the lines in the resulting file (or removing duplicate lines), this is another method:

    sort -u file_with_blank_lines | tail -n +2

Leave a Reply

Your email address will not be published. Required fields are marked *