How to Execute Multiple Sed Commands

9
0 Like Home About Free eBook Archives Best of the Blog Contact Unix Sed Tutorial: How To Execute Multiple Sed Commands by Sasikala on October 16, 2009 Tweet 0 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) Ads by Google Free Dating Dating Online Dating Websites Dating Site Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu... 1 of 9 1/12/2012 12:17 AM

description

unix shell script sed editor execute multiple commands

Transcript of How to Execute Multiple Sed Commands

Page 1: How to Execute Multiple Sed Commands

0 Like

HomeAboutFree eBookArchivesBest of the BlogContact

Unix Sed Tutorial: How To Execute Multiple Sed

Commands

by Sasikala on October 16, 2009

Tweet 0

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

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

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 whileprocessing 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)

Ads by Google Free Dating Dating Online Dating Websites Dating Site

Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

1 of 9 1/12/2012 12:17 AM

Page 2: How to Execute Multiple Sed Commands

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

Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

2 of 9 1/12/2012 12:17 AM

Page 3: How to Execute Multiple Sed Commands

0 Tweet 0 Like Share

9. Software Development

Comment

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

50 Linux Sysadmin Tutorials1.50 Most Frequently Used Linux Commands (WithExamples)

2.

Top 25 Best Linux Performance Monitoring andDebugging Tools

3.

Mommy, I found it! – 15 Practical Linux FindCommand Examples

4.

Linux 101 Hacks 2nd Edition eBook 5.

Awk Introduction – 7 Awk Print ExamplesAdvanced Sed Substitution Examples8 Essential Vim Editor NavigationFundamentals25 Most Frequently Used Linux IPTablesRules ExamplesTurbocharge PuTTY with 12 PowerfulAdd-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

Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

3 of 9 1/12/2012 12:17 AM

Page 4: How to Execute Multiple Sed Commands

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 tounderstand 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 3a 2 3 4a 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$!Ns/^\(\([ \̂n]*\n\)\{49\}\)a\([ \̂n]*\)/\1x\3/t$!b loop‘ filename

5 frankhuang November 17, 2010 at 1:10 am

The commandsed ’1~50s/a/x/g’ filename.txt >out.txt

Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

4 of 9 1/12/2012 12:17 AM

Page 5: How to Execute Multiple Sed Commands

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 [email protected] Sign UpSign UpSign UpSign Up

RSS Twitter Facebook

Search

EBOOKS

Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

5 of 9 1/12/2012 12:17 AM

Page 6: How to Execute Multiple Sed Commands

Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

6 of 9 1/12/2012 12:17 AM

Page 7: How to Execute Multiple Sed Commands

POPULAR POSTS

12 Amazing and Essential Linux Books To Enrich Your Brain and Library50 UNIX / Linux Sysadmin Tutorials50 Most Frequently Used UNIX / Linux Commands (With Examples)How To Be Productive and Get Things Done Using GTD30 Things To Do When you are Bored and have a ComputerLinux Directory Structure (File System Structure) Explained with ExamplesLinux Crontab: 15 Awesome Cron Job ExamplesGet a Grip on the Grep! – 15 Practical Grep Command ExamplesUnix LS Command: 15 Practical Examples15 Examples To Master Linux Command Line HistoryTop 10 Open Source Bug Tracking SystemVi and Vim Macro Tutorial: How To Record and PlayMommy, I found it! -- 15 Practical Linux Find Command Examples15 Awesome Gmail Tips and Tricks15 Awesome Google Search Tips and TricksRAID 0, RAID 1, RAID 5, RAID 10 Explained with DiagramsCan You Top This? 15 Practical Linux Top Command ExamplesTop 5 Best System Monitoring ToolsTop 5 Best Linux OS DistributionsHow To Monitor Remote Linux Host using Nagios 3.0Awk Introduction Tutorial – 7 Awk Print ExamplesHow to Backup Linux? 15 rsync Command ExamplesThe Ultimate Wget Download Guide With 15 Awesome ExamplesTop 5 Best Linux Text EditorsPacket Analyzer: 15 TCPDUMP Command ExamplesThe Ultimate Bash Array Tutorial with 15 Examples3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-idUnix Sed Tutorial: Advanced Sed Substitution ExamplesUNIX / Linux: 10 Netstat Command ExamplesThe Ultimate Guide for Creating Strong Passwords6 Steps to Secure Your Home Wireless NetworkTurbocharge PuTTY with 12 Powerful Add-Ons

Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

7 of 9 1/12/2012 12:17 AM

Page 8: How to Execute Multiple Sed Commands

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 writearticles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan andthe blog.

Support Us

Support this blog by purchasing one of my ebooks.

Bash 101 Hacks eBook

Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

8 of 9 1/12/2012 12:17 AM

Page 9: How to Execute Multiple Sed Commands

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 aboutthis site. You can also simply drop me a line to say hello!.

Follow us on Twitter

Become a fan on Facebook

Copyright © 2008–2012 Ramesh Natarajan. All rights reserved | Terms of Service | Advertise

Unix Sed Tutorial: How To Execute Multiple Sed Commands http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-execute-mu...

9 of 9 1/12/2012 12:17 AM