Sunteți pe pagina 1din 9

Unix Sed Tutorial: How To Execute Multiple Sed Commands

1 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

Home
About
Free eBook
Archives
Best of the Blog
Contact

Free Dating

Ads by Google

Dating Online

Dating Websites

Dating Site

Unix Sed Tutorial: How To Execute Multiple Sed


Commands
by Sasikala on October 16, 2009
0

Like

Tweet

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)

1/12/2012 12:17 AM

Unix Sed Tutorial: How To Execute Multiple Sed Commands

2 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

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'
5. Storage
9. Software Development

-e '/Storage/p'

thegeekstuff.txt

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

1/12/2012 12:17 AM

Unix Sed Tutorial: How To Execute Multiple Sed Commands

3 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

9. Software Development

Tweet

Like

Share

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

Awk Introduction 7 Awk Print Examples


Advanced Sed Substitution Examples
8 Essential Vim Editor Navigation
Fundamentals
25 Most Frequently Used Linux IPTables
Rules Examples
Turbocharge PuTTY with 12 Powerful
Add-Ons

Tags: Sed Multicommand, Sed option -e, Sed Tips and Tricks, Unix Sed Command
{ 5 comments read them below or add one }
1 Guy Patterson October 22, 2009 at 8:43 pm

1/12/2012 12:17 AM

Unix Sed Tutorial: How To Execute Multiple Sed Commands

4 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

Theres 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/domainaccess-$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:
a123
a234
a569
..
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

1/12/2012 12:17 AM

Unix Sed Tutorial: How To Execute Multiple Sed Commands

5 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

also works
Leave a Comment
Name
E-mail
Website

Notify me of followup comments via e-mail

Previous post: Do You Like to Perform Vi Style Editing in BASH Command Line ?
Next post: File Manipulation Examples Using Tac, Rev, Paste, and Join Unix Commands
Sign up for our free email newsletter you@address.com
RSS

Twitter

Sign Up

Facebook

Search

EBOOKS

1/12/2012 12:17 AM

Unix Sed Tutorial: How To Execute Multiple Sed Commands

6 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

1/12/2012 12:17 AM

Unix Sed Tutorial: How To Execute Multiple Sed Commands

7 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

POPULAR POSTS
12 Amazing and Essential Linux Books To Enrich Your Brain and Library
50 UNIX / Linux Sysadmin Tutorials
50 Most Frequently Used UNIX / Linux Commands (With Examples)
How To Be Productive and Get Things Done Using GTD
30 Things To Do When you are Bored and have a Computer
Linux Directory Structure (File System Structure) Explained with Examples
Linux Crontab: 15 Awesome Cron Job Examples
Get a Grip on the Grep! 15 Practical Grep Command Examples
Unix LS Command: 15 Practical Examples
15 Examples To Master Linux Command Line History
Top 10 Open Source Bug Tracking System
Vi and Vim Macro Tutorial: How To Record and Play
Mommy, I found it! -- 15 Practical Linux Find Command Examples
15 Awesome Gmail Tips and Tricks
15 Awesome Google Search Tips and Tricks
RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams
Can You Top This? 15 Practical Linux Top Command Examples
Top 5 Best System Monitoring Tools
Top 5 Best Linux OS Distributions
How To Monitor Remote Linux Host using Nagios 3.0
Awk Introduction Tutorial 7 Awk Print Examples
How to Backup Linux? 15 rsync Command Examples
The Ultimate Wget Download Guide With 15 Awesome Examples
Top 5 Best Linux Text Editors
Packet Analyzer: 15 TCPDUMP Command Examples
The Ultimate Bash Array Tutorial with 15 Examples
3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
Unix Sed Tutorial: Advanced Sed Substitution Examples
UNIX / Linux: 10 Netstat Command Examples
The Ultimate Guide for Creating Strong Passwords
6 Steps to Secure Your Home Wireless Network
Turbocharge PuTTY with 12 Powerful Add-Ons

1/12/2012 12:17 AM

Unix Sed Tutorial: How To Execute Multiple Sed Commands

8 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to,


troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write
articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and
the blog.

Support Us
Support this blog by purchasing one of my ebooks.
Bash 101 Hacks eBook

1/12/2012 12:17 AM

Unix Sed Tutorial: How To Execute Multiple Sed Commands

9 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

Sed and Awk 101 Hacks eBook


Vim 101 Hacks eBook
Nagios Core 3 eBook

Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions about
this site. You can also simply drop me a line to say hello!.
Follow us on Twitter
Become a fan on Facebook
Copyright 20082012 Ramesh Natarajan. All rights reserved | Terms of Service | Advertise

1/12/2012 12:17 AM

S-ar putea să vă placă și