sed tip: Remove / Delete All Leading Blank Spaces / Tabs ( whitespace ) From Each Line

by on December 30, 2007 · 13 comments· LAST UPDATED May 1, 2008

in , ,

The sed (Stream Editor) is very powerful tool. Each line of input is copied into a pattern space. You can run editing commands on each input line to delete or change the input. For example, delete lines containing word DVD, enter:
cat input.txt | sed '/DVD/d'

To Print the lines between each pair of words pen and pencil, inclusive, enter:
$ cat input.txt sed -e '/^PEN/,/^PENCIL/p'

To remove all blank lines, enter:
$ cat /etc/rssh.conf | sed '/^$/d' > /tmp/output.file

sed is very handy tool for editing and deleting unwanted stuff. Following echo statements printed lots of whitespace from left side:
echo "     This is a test"

         This is a test

To remove all whitespace (including tabs) from left to first word, enter:
echo "     This is a test" | sed -e 's/^[ \t]*//'

This is a test


  • s/ : Substitute command ~ replacement for pattern (^[ \t]*) on each addressed line
  • ^[ \t]* : Search pattern ( ^ - start of the line; [ \t]* match one or more blank spaces including tab)
  • // : Replace (delete) all matched pattern

Following sample script reads some data from text file and generate a formatted output. It delete all leading whitespace from front of each line so that text get aligned to left:

exec 3<&0
exec 0<$FILE
while read line
	url=$(echo "http://${DOMAIN}${line}")
        title="$(lynx -dump -source ${url} | grep '<title>' | awk -F '<title>' '{ print $2 }' | cut -d'<' -f1|sed 's/^[ \t]*//')"
        echo "<li>${title}</li>"
exec 0<&3

To delete trailing whitespace from end of each line, enter:
$ cat input.txt | sed 's/[ \t]*$//' > output.txt
Better remove all leading and trailing whitespace from end of each line:
$ cat input.txt | sed 's/^[ \t]*//;s/[ \t]*$//' > output.txt

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

{ 13 comments… read them below or add one }

1 Hannes December 30, 2007 at 2:54 pm is a very handy document for sed too.

2 nixCraft December 30, 2007 at 4:16 pm

Thanks for sharing the link

3 Bijay Rungta December 26, 2008 at 10:23 am

Another great resource:
A list of Commands very well written

4 Anton March 23, 2009 at 12:21 pm

Please stop masturbating and use expand/unexpand.

5 dmadden January 27, 2010 at 9:51 am

Some nice examples, but there’s unnecessary use of a cat process for the examples with input.txt. For example,
cat input.txt | sed ‘/DVD/d’ is more efficient as sed ‘/DVD/d’ input.txt
$ cat input.txt sed -e ‘/^PEN/,/^PENCIL/p’ is more efficent as sed -e ‘/^PEN/,/^PENCIL/p’ input.txt

6 tjac March 7, 2011 at 10:02 pm

perl -pi -e ‘s,[\r\n],,g’ file.xml

7 Naren April 5, 2011 at 12:50 pm

Thanks for sharing …

8 yesuf September 9, 2011 at 1:56 pm

better example yet….

cat /usr/bin/scrub
# Remove all comments and empty lines
sed -e ‘s/#.*//’ -e ‘s/[ ^I]*$//’ -e ‘/^$/ d’ $1

cat /usr/bin/clrspace
# Remove all leading and trailing whitespace from each line;
sed ‘s/^[ \t]*//;s/[ \t]*$//’ $1

now you can simply run
scrub filename
clrspace filename

9 Mark March 30, 2012 at 11:16 am

I tried your sed syntax for leading/trailing space/tabs removal and it doesn’t do it as expected:

# sed s/$/G/g test_file
/ G
/ /dgdkjgd G
/ /fff/f/ff G
/sdfsfssf G
/sfsd/sfd / G
/sfsd/sfdsfs /dfgf/df/G
/dfgdg /gddd /G
# sed ‘s/^[ \t]*//;s/[ \t]*$//’ test_file|sed s/$/G/g
/ /dgdkjgd G
/ /fff/f/ff G
/sfsd/sfd / G
/sfsd/sfdsfs /dfgf/df/G
/dfgdg /gddd /G

10 Mark March 30, 2012 at 11:22 am

ok, I found another solution for it:

sed ‘s/^[[:space:]]*//;s/[[:space:]]*$//’

did the trick.

11 Jimmy7 July 8, 2012 at 3:58 pm

how can I remove spaces at the end of each line in a *.txt file…e.g.
word n[space]
commands you stated didnt work

12 Yahia November 28, 2012 at 1:08 pm

sed -i ‘s/\W*$//gi’ FILENAME

13 gg January 31, 2014 at 5:51 am

sed ‘s/[ \t]*$//g’

Leave a Comment

Tagged as: , , , , , , ,

Previous post:

Next post: