sed Tip: Delete All Blank White Spaces

by on June 9, 2013 · 5 comments· LAST UPDATED June 9, 2013

in , , ,

I have a text file as follows:

   foo
   bar
  foobar 

How can I delete all leading and/or trailing blank spaces, tab from each line using sed command?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementssed/awk/perl
Estimated completion time2 minutes
You can use sed command to delete all white (blank) spaces in a text file. You can also use other text processing utilities such as:

  1. Perl.
  2. Python.
  3. Awk and friends.

Perl example

The syntax is:

perl -lape 's/\s+//sg' input > output

Sample outputs:

foo
bar
foobar

Or use -pie syntax to update file:

 
cat input
perl -lapi -e 's/\s+|^\n//sg'  input
cat input
 

See perl man page for more information.

Sed example

The syntax is:

sed -e 's/^[ \t]*//' -e 's/[ \t]*$//' input > output

OR updated file in a single go with the -i option:

sed -i -e 's/^[ \t]*//' -e 's/[ \t]*$//' input

See sed command man page for more information.

Awk example

The syntax is

awk '{$1=$1}{ print }' input > output

You can also use gsub() substring matching the regular expression function. See awk man page for more information.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 5 comments… read them below or add one }

1 meow June 9, 2013 at 2:10 pm

It seems generally you should choose perl over sed or awk if you don’t care about speed too much. Because you probably would be tempted to use some advanced features only provided by GNU sed and awk, which not likely appearing in OS X or other BSD variants by default.

Reply

2 Jalal Hajigholamali June 10, 2013 at 2:15 am

Hi,

Thanks for nice article.

Reply

3 oll June 11, 2013 at 2:49 pm

The tr command is also usefull.
For instance, I use it this way in a video conversion script to strip – actually replace them with underscore, but you can really strip them with tr – all annoying characters from a filename (often generated by windows) :
goodfilename=`echo -n ${badfilename}|tr ‘[:space:] ( ) &’ _`

If badfilename is for instance :
Me & My Children during Christmas (short version).mp4
It will be converted to
Me___My_Children_during_Chritsmas_short_version_.mp4
which avoid further errors in script.

Reply

4 gallan August 2, 2013 at 10:42 pm

Hi,

Thanks a lot for the command. Sed command worked perfectly.

can you please explain me the way the command worked ?. Sorry if i am annoying most of you ppl here with this silly question but i am serioulsy curious to know how it worked.i am very new to linux and scripting , couldn’t figure out how it worked.

thanks a lot for the command again.would be very greatful if my query is answered.

cheers

Reply

5 Gayathri February 21, 2014 at 3:51 pm

I have the below file,

TEST.DAT –> contain all spaces

I need to remove all the spaces

Please let me know what is the command i can use

and am using tail -1 TEST.DAT as a variable $TEST

and then check $TEST is empty by removing all the spaces

so the unix command should exit if $TEST is blank

Reply

Leave a Comment

Tagged as: , ,

Previous Faq:

Next Faq: