Sie sind auf Seite 1von 13

Home

About
Free eBook
Archives
Best of the Blog
Contact
Subscribe
HowTo & FAQ

Your one-stop shop for networking, routers, wireless printers - Connected Home at
Amazon.
Unix Sed Tutorial: Append, Insert,
Replace, and Count File Lines
by Sasikala on November 9, 2009
This article is part of the on going Unix sed
command tutorial series. In our previous articles we learned sed with single commands
printing, deletion, substitute and le write.
www.spl unk.com/uni xandl i nux Ads by Googl e
Ads by Google Unix Perl Scripts Unix Shell Commands 3D Modeling Tutorial Unix Tutorial
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
1 of 13 11/28/2009 12:10 AM
Sed provides lot of commands to perform number of operations with the lines in a file.
In this article let us review how to append, insert, replace a line in a file and how to
get line numbers of a file.
Append Lines
Insert Lines
Replace Lines
Count Lines
Let us first create thegeekstuff.txt file that will be used in all the examples mentioned
below.
$cat thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Append Lines Using Sed Command
Sed provides the command a which appends a line after every line with the address
or pattern.
Syntax:
#sed 'ADDRESS a\
Line which you want to append' filename
#sed '/PATTERN/ a\
Line which you want to append' filename
Sed Append Example 1. Add a line after the 3rd line of the file.
www.spl unk.com/uni xandl i nux Ads by Googl e
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
2 of 13 11/28/2009 12:10 AM
Add the line Cool gadgets and websites after the 3rd line. sed a command inserts
the line after match.
$ sed '3 a\
> Cool gadgets and websites' thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Cool gadgets and websites
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Sed Append Example 2. Append a line after every line matching the
pattern
The below sed command will add the line Linux Scripting after every line that
matches the pattern Sysadmin.
$ sed '/Sysadmin/a \
> Linux Scripting' thegeekstuff.txt
Linux Sysadmin
Linux Scripting
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Linux Scripting
Sed Append Example 3. Append a line at the end of the file
The following example, appends the line Website Design at the end of the le.
$ sed '$ a\
> Website Design' thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Website Design
Insert Lines Using Sed Command
Sed command i is used to insert a line before every line with the range or pattern.
Syntax:
#sed 'ADDRESS i\
Line which you want to insert' filename
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
3 of 13 11/28/2009 12:10 AM
#sed '/PATTERN/ i\
Line which you want to insert' filename
Sed Insert Example 1. Add a line before the 4th line of the line.
Add a line Cool gadgets and websites before 4th line. a command inserts the line
after match whereas i inserts before match.
$ sed '4 i\
> Cool gadgets and websites' thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Cool gadgets and websites
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Sed Insert Example 2. Insert a line before every line with the pattern
The below sed command will add a line Linux Scripting before every line that
matches with the pattern called Sysadmin.
$ sed '/Sysadmin/i \
> Linux Scripting' thegeekstuff.txt
Linux Scripting
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Linux Scripting
Windows- Sysadmin, reboot etc.
Sed Insert Example 3. Insert a line before the last line of the file.
Append a line Website Design before the last line of the le.
$ sed '$ i\
> Website Design' thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Website Design
Windows- Sysadmin, reboot etc.
Replace Lines Using Sed Command
c command in sed used to replace every line matches with the pattern or ranges with
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
4 of 13 11/28/2009 12:10 AM
the new given line.
Syntax:
#sed 'ADDRESS c\
new line' filename
#sed '/PATTERN/ c\
new line' filename
Sed Replace Example 1. Replace a first line of the file
The below command replaces the rst line of the le with the The Geek Stu.
$ sed '1 c\
> The Geek Stuff' thegeekstuff.txt
The Geek Stuff
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Sed Replace Example 2. Replace a line which matches the pattern
Replace everyline which has a pattern Linux Sysadmin to Linux Sysadmin
Scripting.
$ sed '/Linux Sysadmin/c \
> Linux Sysadmin - Scripting' thegeekstuff.txt
Linux Sysadmin - Scripting
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Linux Fundamentals Class
Hands-on 2-day class in San Jose December 10 -
11, $700 Linux class
LinuxCertified.com/LinuxTraining
Linux/Unix Email Archving
No software installation. Web-based Powerful
Queries. 10-gig Free!
www.mxsense.com
Altus AMD Servers
AMD Opteron Based For Linux Clustering-
Configure & Learn More!
www.penguincomputing.com
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
5 of 13 11/28/2009 12:10 AM
Sed Replace Example 3. Replace the last line of the file
Sed command given below replaces the last line of the le with Last Line of the le.
$ sed '$ c\
> Last line of the file' thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Last line of the file
Print Line Numbers Using Sed Command
= is a command in sed to print the current line number to the standard output.
Syntax:
#sed '=' filename
The above send command syntax prints line number in the first line and the original
line from the file in the next line .
sed = command accepts only one address, so if you want to print line number for a
range of lines, you must use the curly braces.
Syntax:
# sed -n '/PATTERN/,/PATTERN/ {
=
p
}' filename
Sed Line Number Example 1. Find the line number which contains
the pattern
The below sed command prints the line number for which matches with the pattern
Databases
$ sed -n '/Databases/=' thegeekstuff.txt
2
Sed Line Number Example 2. Printing Range of line numbers
Print the line numbers for the lines matches from the pattern Oracle to
Productivity.
$ sed -n '/Oracle/,/Productivity/{
> =
> p
> }' thegeekstuff.txt
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
6 of 13 11/28/2009 12:10 AM
2
Databases - Oracle, mySQL etc.
3
Security (Firewall, Network, Online Security etc)
4
Storage in Linux
5
Productivity (Too many technologies to explore, not much time available)
Sed Line Number Example 3. Print the total number of lines in a file
Line number of the last line of the file will be the total lines in a file. Pattern $ specifies
the last line of the file.
$ sed -n '$=' thegeekstuff.txt
6
Download Free eBook - Linux 101 Hacks
Get free Unix tutorials, tips and tricks straight to your email in-box.
you@address.com Subscribe
Bookmark or Share
Leave a Comment
Print Friendly
RSS Feed
eBook and More Awesome Articles
Download Vim 101
Hacks

Unix Sed Tutorial: Delete File Lines Using Address and
Patterns
1.
Unix Sed Tutorial: Find and Replace Text Inside a File
Using RegEx
2.
Unix Sed Tutorial: Printing File Lines using Address
and Patterns
3.
Unix Sed Tutorial: Multi-Line File Operation with 6
Practical Examples
4.
Unix Sed Tutorial: How To Write to a File Using Sed 5.
Tags: Linux Sed Command, Sed Command, Sed Examples, Sed File Manipulation
Commands, Sed Tips and Tricks, Unix Sed Command
{ 1 trackback }
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
7 of 13 11/28/2009 12:10 AM
Destillat KW46-2009 | duetsch.info - GNU/Linux, Open Source, Softwareentwicklung,
Selbstmanagement, Vim ...
November 13, 2009 at 12:42 am
{ 1 comment read it below or add one }
1 linuxindetails November 12, 2009 at 3:31 am
very nice tutorial !
With Debian version of sed, you need to use the option -i to really append lines in
your le.By default, the modication appears on the standard output.
Leave a Comment
Name
E-mail
Website
Notify me of followup comments via e-mail
Submit
Previous post: How To Execute Ping Command Only For N number of Packets
Next post: Ubuntu Tips: How To View System Log Files in GUI
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
8 of 13 11/28/2009 12:10 AM
Sign up for our free email newsletter you@address.com Sign Up
Follow us on Twitter
Subscribe via RSS
Your Future Is Waiting At
University of Phoenix!
Struggling Financially Due To
Overwhelming Debt?
Premium Web Hosting Services
advertise here
VIM 101 HACKS EBOOK
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
9 of 13 11/28/2009 12:10 AM
Download eBook
"Vim offers just about everything you could ever want from
an editor. The best that can happen is when an experienced
user shows you the way and accompanies you as you learn.
This book does exactly this. "
Prof. Dr. Fritz Mehner (Author of several Vim plugins)
Search
POPULAR POSTS
Get a Grip on the Grep! - 15 Practical Grep Command Examples
Linux 101 Hacks - Download Free eBook
6 Steps to Secure Your Home Wireless Network
Backup and Restore MySQL Database Using mysqldump
Linux Crontab: 15 Awesome Cron Job Examples
Turbocharge PuTTY with 12 Powerful Add-Ons - Software for Geeks #3
Mommy, I found it! -- 15 Practical Linux Find Command Examples
Unix LS Command: 15 Practical Examples
How To Monitor Remote Windows Machine Using Nagios on Linux
15 Examples To Master Linux Command Line History
CATEGORIES
Vi / Vim Tips and Tricks
Linux Tutorials
SSH Tips and Tricks

Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
10 of 13 11/28/2009 12:10 AM
Productivity Tips
HowTo & FAQ
Hardware Articles
Nagios 3.0 Tutorials
MySQL
PostgreSQL
Oracle
12 AMAZING LINUX BOOKS
1. Sed and Awk
2. Learning the Vi and Vim Editors
3. Bash Cookbook
4. SSH, The Secure Shell
5. Essential System Administration
6. Linux Server Hacks, Volume One
7. DNS and BIND
8. Understanding the Linux Kernel
9. Linux Cookbook
10. Linux Firewalls
11. Linux Administration Handbook
12. Beginning Ubuntu Linux
Read full review of these 12 books
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
11 of 13 11/28/2009 12:10 AM
Sponsored Links
File Name
Ruby Script
Mastering Regular
Expressions
Unix Shell
Shell Script
Microsoft SQL Server
Sample Code
Line Number
search
ContextLinks by NetSeer
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.
Networking
Follow us on Twitter
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
12 of 13 11/28/2009 12:10 AM
Facebook
Contact Us
Contact Me : Use this Contact Form to get in touch me for your comments,
questions or suggestions about this site. You can also simply drop me a line to say
hello!.
Send Your Tips: If you like to share any awesome tips and tricks on technology
topic use the contact form to get in touch with me.
Mobile Version: Go to m.thegeekstuff.com on your mobile to access this blog
from your phone.
Copyright 20082009 Ramesh Natarajan. All rights reserved | Terms of Service
Advertise | Questions or Comments
Unix Sed Tutorial: Append, Insert, Replace, and C... http://www.thegeekstu.com/2009/11/unix-sed-tutor...
13 of 13 11/28/2009 12:10 AM

Das könnte Ihnen auch gefallen