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. In this quick tip, I will explain how to remove all leading blank spaces using the sed command. For example, to delete lines containing word DVD, enter the following sed command:
$ cat input.txt | sed '/DVD/d'

Advertisement

To Print the lines between each pair of words pen and pencil, inclusive, enter:

Let us use the combination of the cat command and sed command:
$ cat input.txt sed -e '/^PEN/,/^PENCIL/p'
You can skip the cat and use it as follows:
$ sed -e '/^PEN/,/^PENCIL/p' my_input.txt > my_output.txt
It is possible to use the grep command or egrep command too. For example, find all lines contain word “Linux” and then run sed over it:
$ grep "Linux" file.txt | sed 's/^[ \t]*//'

To remove all blank lines, enter:

Let us try to remove all empty lines from a text file:
$ cat /etc/rssh.conf | sed '/^$/d' > /tmp/output.file

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

The 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"
Sample outputs:

         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]*//'
Here is what I get:

This is a test

Where,

  • 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

Here is my demo.txt file and I am removing all leading white spaces:
$ more demo.txt
My messed up file:

        0|BIOS
            1|System
              2|Baseboard
3|Chassis
            4|Processor
5|Memory Controller
                6|Memory Module
7|Cache
            8|Port Connector
                    9|System Slots
   10|On Board Devices

Now:
$ sed 's/^[ \t]*//' demo.txt
# let us go fancy with the bat command #
$ sed 's/^[ \t]*//' demo.txt | bat
$ sed 's/^[ \t]*//' demo.txt | batcat

Removing all leading blank spaces or tabs ( whitespace ) using sed on Linux Unix

Removing all leading blank spaces or tabs ( whitespace ) using sed on Linux Unix (clicl to enlarge)

Shell script example

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:

#!/bin/bash
FILE=url.dump.txt
DOMAIN=yourdomain.com
exec 3<&0
exec 0<$FILE
while read line
do
	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>"
done
exec 0<&3

To delete trailing whitespace from end of each line, enter:

I use the cat command out of habits but it is not needed:
$ 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

Summing up

These are some quick examples for sed commands when you wish to delete or remove all leading blank spaces/tabs ( whitespace ) from each input line on your Linux, macOS, FreeBSD or Unix machine. Do read the following manual page using the man command or help command:
$ man sed

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

14 comments… add one
  • Hannes Dec 30, 2007 @ 14:54

    http://sed.sourceforge.net/sed1line.txt is a very handy document for sed too.

  • 🛡️ Vivek Gite (Author and Admin) nixCraft Dec 30, 2007 @ 16:16

    Thanks for sharing the link

  • Bijay Rungta Dec 26, 2008 @ 10:23

    Another great resource:
    A list of Commands very well written
    http://student.northpark.edu/pemente/sed/sed1line.txt

  • Anton Mar 23, 2009 @ 12:21

    Please stop masturbating and use expand/unexpand.

  • 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
  • tjac Mar 7, 2011 @ 22:02

    Perl:

    perl -pi -e 's,[rn],,g' file.xml
  • Naren Apr 5, 2011 @ 12:50

    Thanks for sharing …

  • yesuf Sep 9, 2011 @ 13:56

    Better example yet. From:
    $ 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
  • 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
    Outputs:

    /etcG
    /homeG
    /usr/sbin/snapshotsG
    /usr/local/etcG
    /var/spool/cron/crontabsG
    G
    /   G
    /               /dgdkjgd         G
    /G
    / /fff/f/ff                     G
    G
       /sdfsfssf     G
             #/sfsfsfG
    /sfsd/sfd /                    G
    /sfsd/sfdsfs /dfgf/df/G
    /dfgdg /gddd /G
    # sed 's/^[ t]*//;s/[ t]*$//' test_file|sed s/$/G/g
    /etcG
    /homeG
    /usr/sbin/snapshotsG
    /usr/local/etcG
    /var/spool/cron/crontabsG
    G
    /G
    /               /dgdkjgd        G
    /G
    / /fff/f/ff                     G
    G
    /sdfsfssfG
    #/sfsfsfG
    /sfsd/sfd /             G
    /sfsd/sfdsfs /dfgf/df/G
    /dfgdg /gddd /G
    #
  • Mark Mar 30, 2012 @ 11:22

    ok, I found another solution for it:

    sed 's/^[[:space:]]*//;s/[[:space:]]*$//'

    did the trick.

  • Jimmy7 Jul 8, 2012 @ 15:58

    Hi,
    how can I remove spaces at the end of each line in a *.txt file…e.g.

    word1[space]
    word2[space]
    word3[space]
    .
    .
    word n[space]

    commands you stated didnt work

  • Yahia Nov 28, 2012 @ 13:08

    sed -i 's/W*$//gi' FILENAME

  • gg Jan 31, 2014 @ 5:51

    sed 's/[ t]*$//g'

  • Harris Kool Oct 23, 2014 @ 7:53

    ls *XONE* | wc -w | sed 's/ *//'

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.