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:


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

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
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:


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.

Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 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.


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


Thanks for nice article.


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
which avoid further errors in script.


4 gallan August 2, 2013 at 10:42 pm


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.



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


Leave a Comment

Tagged as: , ,

Previous Faq:

Next Faq: