Howto: Linux command line utilities for removing blank lines from text files

by Vivek Gite · 12 comments

Q. I want to change the formatting of a file. I just wanted to remove all blank lines from text file. How do I achieve this task w/o spending much time?

A. Yes, you do not have to waste your time making manual changes to files. Both Linux and UNIX systems come with file manipulation tools that can be used to remove all blank lines very quickly.

Task: Remove blank lines using sed

Type the following command:
$ sed '/^$/d' input.txt > output.txt

Task: Remove blank lines using grep

$ grep -v '^$' input.txt > output.txt

Both grep and sed use special pattern ^$ that matchs the blank lines. Grep -v option means print all lines except blank line.

Let us say directory /home/me/data/*.txt has all text file. Use following for loop (shell script) to remove all blank lines from all files stored in /home/me/data directory:

#!/bin/sh
files="/home/me/data/*.txt"
for i in $files
do
  sed '/^$/d' $i > $i.out
  mv  $i.out $i
done
 

Updated for accuracy.

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 12 comments… read them below or add one }

1 James Herr 03.13.08 at 5:08 pm

Is that last shell script wrong? The for loop is using variable f, and inside the loop, everything is using variable i. I think the script should look more like:

#!/bin/sh
files=”/home/me/data/*.txt”
# Next line is changed to use variable i, not f
for i in $files
do
sed ‘/^$/d’ $i > $i.out
mv $i.out $i
done

2 Deepak 08.10.08 at 8:40 am

its nice..appreciable

3 vivek 08.10.08 at 9:00 am

James,

Thanks for the heads up. The faq has been updated.

4 Rick 10.28.08 at 5:37 pm

When writing to the same file, or a different file and using the redirect > the file ends up blank.

This removes blank lines from input.txt

sed ‘/^$/d’ -i input.txt

5 Prashant Deshani 11.26.08 at 1:19 pm

Hi,

I tried to use the command sed given above to remove blank line from my file.

The file looks good except one strange behaviour:
“Always” the last line of the file is removed..for example:
test.p is the file and following are the contents of the file:

BEFORE RUNNING THE SED COMMAND:
“this is test 1.

this is test 2.

this is test 3.”

AFTER RUNNING THE SED COMMAND:
“this is test 1.
this is test 2.”

As you can see the last line (“this is test 3.”) is removed after running the command.

If I put the blank line at the end it removes the blank line i.e.
“this is test 1.

this is test 2.

this is test 3.

becomes…….
“this is test 1.
this is test 2.
this is test 3.”

Do let me know what I should do in this case?

Waiting for your prompt reply…!

Thanks and Best Regards,
Prashant Deshani

6 Dennis 01.28.09 at 2:34 pm

Actually, grep’s -v (–invert-match) option inverts the sense of matching, thus selecting non-matching lines.
Therefore, only when combining it with the pattern ‘^$’ does it mean to select everything except blank lines.
Just thought the way you explained it might be confusing for beginners. When I was new to shell and Linux, most of the guides or forums were useless because they all assumed that the person that needs help has some high understanding of shell or Linux already, and thus they use a lot of terms that person would have never heard of before or they don’t explain anything. Once you have a basic understanding, the learning comes easier, but in the beginning, the learning curve has an almost horizontal slope.

7 REDDY SIVA SARAN 04.15.09 at 10:46 am

Hi,
I am Siva Saran
I am new to linux environment
can any on tell plz that how to get a particular line from a file using linux commands
For example say that inputfile has lines as follows
total 0
d [RWCEAFMS] MRohit 512 Apr 15 03:15 3.1_SP1_Beta1_127
d [RWCEAFMS] MRohit 512 Apr 14 03:26 3.1_SP1_125 d [RWCEAFMS] MRohit 512 Apr 14 03:26 3.1_SP1_259
d [RWCEAFMS] MRohit 512 Apr 14 03:26 3.0_SP4_IR3_62

Now I want the line which is having SP1_125 ie., line number 3 and store it in an variable
plz can any tell the answer plz
can u mail to {snip_no_email_ids}

8 Renz 06.18.09 at 6:22 am

what if there’s spaces and tab in the line I want to delete?

thanks,
Renz

9 Niju N B 07.13.09 at 11:47 am

Hi,

Using the command
grep -R “SP1_125″
you can get the lines with SP1_125.
if you want to assign to a variable then use
var_a=$(grep -R “SP1_125″ )

where is the name of file

10 Niju N B 07.13.09 at 11:49 am

var_a = $(grep -R “SP1_125″ <filename>)

11 Niju N B 07.13.09 at 11:52 am

Hi,
var_a=$(grep -R “SP1_125″ filename)

12 ezz 11.04.09 at 4:48 pm

This is quite helpful. Thanks!!

Leave a Comment

You can 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>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All