sed Tip: Delete All Blank White Spaces

in Categories , , , last updated June 9, 2013

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?

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(1) 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(1) 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(1) for more information.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 5 comments/add one below):

5 comment

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

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


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

    Have a question? Post it on our forum!