Let us review how to print file lines using address and patterns in this first part of sed tutorial.
We’ll be posting several awesome sed tutorials with examples in the upcoming weeks.
Unix Sed Introduction
- sed is a “non-interactive” stream-oriented editor. Since its an “non-interactive” it can be used to automate editing if desired.
- The name sed is an abbreviation for stream editor, and the utility derives many of its commands from the ed line-editor (ed was the first UNIX text editor).
- This allows you to edit multiple files, or to perform common editing operations without ever having to open vi or emacs.
- sed reads from a file or from its standard input, and outputs to its standard output.
- sed has two buffers which are called pattern buffer and hold buffer. Both are initially empty.
Unix Sed Working methodology
This is called as one execution cycle. Cycle continues till end of file/input is reached.
- Read a entire line from stdin/file.
- Removes any trailing newline.
- Places the line, in its pattern buffer.
- Modify the pattern buffer according to the supplied commands.
- Print the pattern buffer to stdout.
Printing Operation in Sed
Linux Sed command allows you to print only specific lines based on the line number or pattern matches. “p” is a command for printing the data from the pattern buffer.
To suppress automatic printing of pattern space use -n command with sed. sed -n option will not print anything, unless an explicit request to print is found.
Syntax: # sed -n 'ADDRESS'p filename # sed -n '/PATTERN/p' filename
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.
5 Sed ADDRESS Format Examples
Sed Address Format 1: NUMBER
This will match only Nth line in the input.
# sed -n ‘N’p filename
For example, 3p prints third line of input file thegeekstuff.txt as shown below.
# sed -n '3'p thegeekstuff.txt 3. Hardware
Sed Address Format 2: NUMBER1~NUMBER2
M~N with “p” command prints every Nth line starting from line M.
# sed -n ‘M~N’p filename
For example, 3~2p prints every 2nd line starting from 3rd line as shown below.
# sed -n '3~2'p thegeekstuff.txt 3. Hardware 5. Storage 7. Productivity (Too many technologies to explore, not much time available) 9. Software Development
Sed Address Format 3: START,END
M,N with “p” command prints Mth line to Nth line.
# sed -n ‘M,N’p filename
For example, 4,8p prints from 4th line to 8th line from input file thegeekstuff.txt
# sed -n '4,8'p thegeekstuff.txt 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
Sed Address Format 4: ‘$’ Last Line
$ with “p” command matches only the last line from the input.
# sed -n ‘$’p filename
For example, $p prints only the last line as shown below.
# sed -n '$'p thegeekstuff.txt 10.Windows- Sysadmin, reboot etc.
Sed Address Format 5: NUMBER,$
N,$ with “p” command prints from Nth line to end of file.
# sed -n ‘N,$p’ filename
For example 4,$p prints from 4th line to end of file.
# sed -n '4,$p' thegeekstuff.txt 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.
6 Sed PATTERN Format Examples
Sed Pattern Format 1: PATTERN
PATTERN could be unix regular expression. The below command prints only the line which matches the given pattern.
# sed -n /PATTERN/p filename
For example, following prints the line only which matches the pattern “Sysadmin”.
# sed -n /Sysadmin/p thegeekstuff.txt 1. Linux - Sysadmin, Scripting etc. 10.Windows- Sysadmin, reboot etc.
Sed Pattern Format 2: /PATTERN/,ADDRESS
# sed -n ‘/PATTERN/,Np’ filename
For example, following prints lines which matches the pattern to Nth line, from input. 3rd line matches the pattern “Hardware”, so it prints from 3rd line to 6th line.
# sed -n '/Hardware/,6p' thegeekstuff.txt 3. Hardware 4. Security (Firewall, Network, Online Security etc) 5. Storage 6. Cool gadgets and websites
Sed Pattern Format 3: ADDRESS,/PATTERN/
It prints from the Nth line of the input, to the line which matches the pattern. If the pattern doesnt match, it prints upto end of the input.
# sed -n ‘N,/PATTERN/p’ filename
For example, 4th line matches the pattern “Security”, so it prints from 3rd line to 4th line.
# sed -n '3,/Security/p' thegeekstuff.txt 3. Hardware 4. Security (Firewall, Network, Online Security etc)
Sed Pattern Format 4: /PATTERN/,$
It prints from the line matches the given pattern to end of file.
# sed -n ‘/PATTERN/,$p’ filename
# sed -n '/Website/,$p' thegeekstuff.txt 8. Website Design 9. Software Development 10.Windows- Sysadmin, reboot etc.
Sed Pattern Format 5: /PATTERN/,+N
It prints the lines which matches the pattern and next N lines following the matched line.
# sed -n ‘/PATTERN/,+Np’ filename
For example, following prints the 5th line which matches the pattern /Storage/ and next two lines following /Storage/.
# sed -n '/Storage/,+2p' thegeekstuff.txt 5. Storage 6. Cool gadgets and websites 7. Productivity (Too many technologies to explore, not much time available)
Sed Pattern Format 6: /PATTERN/,/PATTERN/
Prints the section of file between two regular expression (including the matched line ).
# sed -n ‘/P1/,/P2/p’ filename
For example, 5th line matches “Storage” and 8th line matches “Design”, so it prints 5th to 8th.
# sed -n '/Storage/,/Design/p' thegeekstuff.txt 5. Storage 6. Cool gadgets and websites 7. Productivity (Too many technologies to explore, not much time available) 8. Website Design
Comments on this entry are closed.
This is a great addition to my bag of tricks. I knew about grep to find patterns, but sed looks like a good way to fix things in multiple files. Thanks!
format #2
invalid command code ~
(FreeBSD 7.0-RELEASE)
# sed -n ‘4,’p file
sed: 1: “4,p”: expected context address
oh, sorry, my mistake =)
# sed -n ‘4,$’p file
~> sed -n ‘3~2’p thegeekstuff.txt
sed: invalid command code ~
but:
~> gsed -n ‘3~2’p thegeekstuff.txt
works fine. (gsed = GNU sed)
# — variant for UNIX (non GNU) sed:
~> sed -n ‘3,${p;n;}’ thegeekstuff.txt
prints every 2nd line starting from 3rd
~> sed -n ‘3,${p;n;n;}’ thegeekstuff.txt
prints every 3rd line starting from 3rd
Useful link:
http://sed.sourceforge.net/sed1line.txt
awesome tricks with 1-line scripts for sed
(http://ant0.ru/sed1line.html – russian version of sed1line document)
Excellent tutorial.
However, in the very last example, the match is on ‘Design’, not on ‘Website’.
Usefull Article, thanks Ramesh…
Hi,
Very good tutorial. Really it helped me a lot. Thank u sooooooo much.
Very good tutorial for sed. Thank you very much.
For the below command the output is not being limited to N’th (6th) line.
$sed -n ‘/Sysadmin/,6p’ 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
10.Windows- Sysadmin, reboot etc.
================================
Please let me know where i am doing wrong.
@Harsh,
The given command will print the lines which matches for the word ‘Sysadmin’ to 6th line in a file (if available) . Since first line matches for the word ‘Sysadmin’ it prints the first line to 6th line. And 10th line (last) line also matches to the given word, so it prints.
Hi~
These examples are very useful.
I wonder how to apply “sed” to print the following format.
input data output data
a1 b1 a1 b1 a3 b3 a5 b5 a1 b1 b3 b5
a2 b2 —-> a2 b2 a4 b4 a6 b6 —-> a2 b2 b4 b6
a3 b3
a4 b4
a5 b5
a6 b6
Thank you in advance 🙂
Very helpful!
In an organisation one wants to know how many programmers are there. The employee data is stored in a file called ‘personnel’ with one record per employee. Every record has field for designation. How can sed be used to print only the records of all employees who are programmers ?
How to find last 10 lines if I don’t know size of file?
It’s helping a lot ……thank uuuuuuuuuuuuuu
Hi ramesh
whats wrong with this code?
=========
I want to use sed for printing a file line by line
sorry for foolish question
the answer is :
for ((i=1;i> newfile
done
for ((i=1;i<10;i++))
do
sed -n ' '$i' 'p newfile
done
http://www.paste.to/MTEwNDE2
Hi,
There are some problems I am facing with linux right now.
This is what I am doing to generate a cryptographic signature fron bash script….
step1. I encrypt a text : encrypt(bhavyakailkhurabhavyakailkhura) and get a encrypted string sdfghjklsdfghjklzxcvbnmghjkdfghjkzxcvbnmzxc
step2. I copy that string manually from mouse no commands used and append it with a file..
$ cat textfile.txt…
user=bhavya
signature={sdfghjklsdfghjklzxcvbnmghjkdfghjkzxcvbnmzxc}
step3. Using “sed” on text file I send text between “{” and “}” to another file bhavya.txt
step4. now when I try to decrypt bhavya.txt it say error in reading file. I checked the text is same as encrypted. When I copy original encrypted text in step 1 and decrypt It works.
I think the problem is sed command changes spacing between text its not same as copying using mouse. Do you have any idea how we can solve this problem?
Sorry for clumsy and lengthy explanation.
Thanks
I would like to subsitute the comma in the amount enclosed in double quote only and leave all the comma as it. How can I do it?R,112074121151,45891,SMITH,JOHN ,HELLEN,”$1,078.67″,1118.69
Thank you.
Hedy
It’s very helpful to understand what sed is!.
i want to print the last 5 lines of a file using sed command???
hi., i want to know the syntax for printing multiple different lines using sed.
like sed -n ‘2,4’p,’6,8’p thegeekstuff.txt..
want to print lines 2 to 4 and 6 to 8., how to do it..?
Rahul Prasad,
Try this command for emulating tail -5.
sed -e :a -e ‘$q;N;6,$D;ba’ thegeekstuff.txt
Shiva,
You can try this command.
sed -n ‘2,4 p; 6,8 p’ thegeekstuff.txt
or
sed -n -e ‘2,4 p’ -e ‘6,8 p’ thegeekstuff.txt
I just started learning sed and I am glad I found this article. You made it very illustrative with all the examples. Thank you
Hi All,
How to print 3th line first and 2nd line next??
3. Hardware
2. Databases – Oracle, mySQL etc.
Hi., i want to change the extension of my all files which are in current dir. via using sed command………….. , i m trying this code in a shell script
#!/bin/bash
for file in *
do
NEW=”echo $file|sed -f ‘s/.txt.new/.txt/g'”
mv $file $NEW
done
when i m running this shell script, o/p is :-
mv: target `\’s/txt.new/txt/g\” is not a directory
Plz fix it……………. thanx in advance………
has anyone got issues with
Sed Pattern Format 6: /PATTERN/,/PATTERN/
Prints the section of file between two regular expression (including the matched line ).
when used in loop it is printing the whole file.
ex:
for i in `cat file` ; do sed ‘/$i/,/$i/p’ filename ;done
my file has 3 entries .for every iteration it is printing all the lines i.e. my output seems to be 3*file conent.
Without using the loop it works fine.
Really Awesome!!!
Really useful 🙂 Thanks for the article
HI Can any one tell me hwo to write a code for below :
From the command zpool status
zpool status -x
pool: tank
state: DEGRADED
status: One or more devices could not be opened. Sufficient replicas exist for
the pool to continue functioning in a degraded state.
action: Attach the missing device and online it using ‘zpool online’.
see: http://www.sun.com/msg/ZFS-8000-D3
scrub: resilver completed with 0 errors on Fri Mar 17 14:38:47 2006
config:
NAME STATE READ WRITE CKSUM
tank DEGRADED 0 0 0
mirror DEGRADED 0 0 0
c1t0d0 UNAVAIL 0 0 0 cannot open
c1t1d0 ONLINE 0 0 0
Here i ahve to select c1t0d0 and then format c1t0d0
can any write script for this
sed -n ‘/PATTERN/,+Np’ filename in not working in Solaris? any alternative
it helped * me alot.
how to delete a line which starts and end with same pattern
It is awesome!!!!