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

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

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
14 comments… add one
  • Harris Kool Oct 23, 2014 @ 7:53

    ls *XONE* | wc -w | sed ‘s/ *//’

  • gg Jan 31, 2014 @ 5:51

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

  • Yahia Nov 28, 2012 @ 13:08

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

  • Jimmy7 Jul 8, 2012 @ 15:58

    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

  • Mark Mar 30, 2012 @ 11:22

    ok, I found another solution for it:

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

    did the trick.

  • Mark Mar 30, 2012 @ 11:16

    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

  • yesuf Sep 9, 2011 @ 13:56

    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

  • Naren Apr 5, 2011 @ 12:50

    Thanks for sharing …

  • tjac Mar 7, 2011 @ 22:02

    perl -pi -e ‘s,[rn],,g’ file.xml

  • dmadden Jan 27, 2010 @ 9:51

    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

  • Anton Mar 23, 2009 @ 12:21

    Please stop masturbating and use expand/unexpand.

  • Bijay Rungta Dec 26, 2008 @ 10:23

    Another great resource:
    A list of Commands very well written

  • 🐧 nixCraft Dec 30, 2007 @ 16:16

    Thanks for sharing the link

  • Hannes Dec 30, 2007 @ 14:54 is a very handy document for sed too.

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @