≡ Menu

Unix Sed Tutorial: How To Execute Multiple Sed Commands

Question: Is it possible for me to combine multiple sed commands? Can I combine two sed commands and execute it as single sed command?

Answer: In our previous articles we learned sed with single commands — printing, deletion, substitute and file write.

In this article let us review how to combine multiple sed commands using option -e as shown below.

Syntax:

#sed -e 'command' -e 'command' filename

Note: -e option is optional for sed with single command. sed will execute the each set of command while processing input from the pattern buffer.

Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.

# cat thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

1.Delete 4th and 2nd line from the input

This sed example deletes 4th and 2nd line from the file thegeekstuff.txt. Using “-e” option, you can give any number of commands with sed.

$ sed -e '4d' -e '2d' thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
3. Hardware
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

2. Print the lines which matches the pattern1 and lines matches pattern2

This sed example prints all lines that matches either the pattern “Storage” or “Software”.

$ sed -n  -e '/Software/p'  -e '/Storage/p'  thegeekstuff.txt
5. Storage
9. Software Development

3. Delete the first,last and all the blank lines from input

This sed example deletes the first line, last line and all the blank lines from input file.

$ sed -e '1d' -e '$d' -e '/^$/d' thegeekstuff.txt
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Guy Patterson October 22, 2009, 8:43 pm

    There’s also potential to use a semi-colon to combine multiple sed arguments.

    For example, I use the following to watch httpd access logs:

    RED=`echo -en ‘\e[32m’`
    YELLOW=`echo -en ‘\e[93m’`
    RESET=`echo -en ‘\e[00m’

    sudo tail -f /mnt/netdata/_shares/logs/httpd/domain.com/$YEAR/$MON/domain-access-$MON-$YEAR.log |sed -e “s/^.*\]: //g;s/ http://www.domain.*\] \”/ – \”/g;s/ HTTP\/1\.[0-9]\”/\”/g;s/ [0-9][0-9][0-9] [0-9]* \”/ \”/g;s/http:\/\/www.domain.com//g;s/\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/$RED\1$RESET/g;s/\(\”[^\”]*\”\)/$YELLOW\1$RESET/g;s/*169.254.254.254*//g”

    yay cli! :]

  • fety November 12, 2009, 3:14 am

    Hi, Ramesh

    I have been following your tutorial from the first tutorial of SED. It is really help me so much to understand better about SED.
    Again, thanks very much.

  • John Alles January 20, 2010, 9:18 am

    Hi,
    I have a file as 12000 lines like this:
    a 1 2 3
    a 2 3 4
    a 5 6 9
    …..
    I would like to change “a” by “x” every 50 lines.

    I know this command:
    sed -e “s///”

    I have to 2400 times.
    Do you know easier way for taht?

  • Sasikala January 20, 2010, 11:16 pm

    To change “a” by “x” in every 50th line of a file, use the following.

    $ sed ‘
    :loop
    $!N
    s/^\(\([^\n]*\n\)\{49\}\)a\([^\n]*\)/\1x\3/
    t
    $!b loop
    ‘ filename

  • frankhuang November 17, 2010, 1:10 am

    The command
    sed ‘1~50s/a/x/g’ filename.txt >out.txt
    also works

  • HuGo March 14, 2013, 12:52 am

    How do i use multiple sed in one file when I change words (i try to make a script). Like:
    sed -e ‘s/cat/dog/g’ file.txt > file2.txt
    sed -e ‘s/one/single/g’ file.txt > file2.txt
    sed -e ‘s/car/bus/g’ file.txt > file2.txt

    So I have a one file and i want another file which is otherwise same except those spesifix words has been changed to anothers?

  • nagasena November 15, 2013, 6:09 am

    HI is it possible to give the pattern to be replaced form a file for sed command.
    the situation wat i have is like
    input_file.txt
    AAA 4
    BBB 5
    CCC 6
    etc
    and i need to replace all these patterns in a output_file.txt