You are on page 1of 9

Unix Sed Tutorial: How To Write to a File Using Sed

1 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-...

Home
About
Free eBook
Archives
Best of the Blog
Contact

Unix Shell Script

Ads by Google

Linux Hosting

Unix Tutorial

Unix or Linux

Unix Sed Tutorial: How To Write to a File Using Sed


by Sasikala on October 7, 2009
0

Like

Tweet

1/12/2012 12:27 AM

Unix Sed Tutorial: How To Write to a File Using Sed

2 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-...

This article is part of Unix Sed Tutorial series. In previous articles,


we discussed about sed print operation , sed delete operation and sed find and replace.
In this article, let us review how to extract part of one file and write it to another file using sed.
Sed provides w command to write the pattern space data to a new file.
Sed creates or truncates the given filename before reads the first input line and it writes all the matches to a file
without closing and re-opening the file.
Syntax:
#sed 'ADDERSSw outputfile' inputfilename
#sed '/PATTERN/w outputfile' inputfilename

Sed reads a line and place it in a pattern buffer and writes the pattern buffer to the given output file according to
the supplied commands.
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.

Let us review some examples of write command in sed.

1. Write 1st line of the file


In this example, 1 (address) refers the first line of the input and w writes the pattern buffer to the output file
output.txt
$ sed -n '1w output.txt' thegeekstuff.txt
$ cat output.txt
1. Linux - Sysadmin, Scripting etc.

2. Write first & last line of the file

1/12/2012 12:27 AM

Unix Sed Tutorial: How To Write to a File Using Sed

3 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-...

In this example, 1 and $ refers first and last line respectively.


$ sed -n -e '1w output.txt' -e '$w output.txt' thegeekstuff.txt
$ cat output.txt
1. Linux - Sysadmin, Scripting etc.
10.Windows- Sysadmin, reboot etc.

3. Write the lines matches with the pattern Storage or Sysadmin


In this example sed command writes the lines which matches the pattern Storage or Sysadmin.
$ sed -n -e '/Storage/w output.txt' -e '/Sysadmin/w output.txt' thegeekstuff.txt
$ cat output.txt
1. Linux - Sysadmin, Scripting etc.
5. Storage
10.Windows- Sysadmin, reboot etc.

4. Write the lines from which the pattern matches to till end of the file
In this example, /Storage/,$ represents line matches from Storage to end of the file.
$ sed -n '/Storage/,$w output.txt' thegeekstuff.txt
$ cat output.txt
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.

5. Write the lines which matches pattern and next two lines from match
In this example, the send command writes the line matches for Storage and two lines next to that.
$ sed -n '/Storage/,+2w output.txt' thegeekstuff.txt
$ cat output.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)

1/12/2012 12:27 AM

Unix Sed Tutorial: How To Write to a File Using Sed

4 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-...

Web Hosting only Rs. 42 www.net4.in


99.9% Uptime, Unlimited Band Width 10 Sub Domains
& 24*7 Tel. Support
Linux VPS Hosting www.pintwire.co.in
512 MB Ram + 50 GB HDD + 1 IP 100 Mbps at Rs 525
Only
Red Hat Linux Training www.cossindia.net
RHCE, RHCSS Training & Exam Best Red Hat training
in Hyderabad
Unix Developer www.IBM.com/Software-Jobs
Expert Testers & Developers Needed. Search by
Location & Experience!

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: Linux Sed Command, Multicommand in Sed, Sed Examples, Sed Tips and Tricks, Unix Sed Command
{ 1 comment read it below or add one }
1 M.Sivagami July 21, 2011 at 7:19 am
Hi,
May i know how to substitute a \n with multiple new lines.

1/12/2012 12:27 AM

Unix Sed Tutorial: How To Write to a File Using Sed

5 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-...

Thanks in advance
Leave a Comment
Name
E-mail
Website

Notify me of followup comments via e-mail

Previous post: Book Review: Perl Best Practices by Damian Conway


Next post: Pascal Hello World Example: How To Write, Compile and Execute Pascal Program on Unix OS
Sign up for our free email newsletter you@address.com
RSS

Twitter

Sign Up

Facebook

Search

EBOOKS

1/12/2012 12:27 AM

Unix Sed Tutorial: How To Write to a File Using Sed

6 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-...

1/12/2012 12:27 AM

Unix Sed Tutorial: How To Write to a File Using Sed

7 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-...

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:27 AM

Unix Sed Tutorial: How To Write to a File Using Sed

8 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-...

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:27 AM

Unix Sed Tutorial: How To Write to a File Using Sed

9 of 9

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-how-to-write-to-a-...

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:27 AM

You might also like