sed Tip: Delete All Blank White Spaces

See all UNIX related articles/faq
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
Difficulty level Easy
Root privileges No
Requirements sed/awk/perl
Est. reading time 1 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 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 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 for more information.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

5 comments… add one
  • meow Jun 9, 2013 @ 14:10

    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.

  • Jalal Hajigholamali Jun 10, 2013 @ 2:15


    Thanks for nice article.

  • oll Jun 11, 2013 @ 14:49

    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.

  • gallan Aug 2, 2013 @ 22:42


    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.


  • Gayathri Feb 21, 2014 @ 15:51

    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 Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.