Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Remove empty lines in a file

Answer

Pick any one of the following methods.

1. grep

grep . test.txt

2. sed

sed '/^$/d' test.txt

3. awk

awk NF test.txt

4. tr

cat test.txt | tr -s "\n"

5. perl

perl -n -e 'print unless /^$/' test.txt

  1. Remove comments (#) and empty lines in a file
  2. Create an empty file in Linux
  3. How to create an empty file in Linux
  4. Count the number of matching lines from grep
  5. How to remove duplicated lines in a file

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>