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

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

{ 7 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

5 frankhuang November 17, 2010 at 1:10 am

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

6 HuGo March 14, 2013 at 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?

7 nagasena November 15, 2013 at 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

Leave a Comment

Previous post:

Next post: