Unix Sed Tutorial: How To Execute Multiple Sed Commands

by Sasikala on October 16, 2009

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
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

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

  1. Unix Sed Tutorial: Delete File Lines Using Address and Patterns
  2. Unix Sed Tutorial: Printing File Lines using Address and Patterns
  3. Unix Sed Tutorial: How To Write to a File Using Sed
  4. Unix Sed Tutorial: Append, Insert, Replace, and Count File Lines
  5. File Manipulation Examples Using Tac, Rev, Paste, and Join Unix Commands
  

Vim 101 Hacks Book

{ 4 comments… read them below or add one }

1 Guy Patterson October 22, 2009 at 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! :]

2 fety November 12, 2009 at 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.

3 John Alles January 20, 2010 at 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?

4 Sasikala January 20, 2010 at 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

Leave a Comment

Previous post:

Next post: