Unix Sed Tutorial: Printing File Lines using Address and Patterns

by Sasikala on September 14, 2009

Linux Sed Tutorial - Printing File LinesLet 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.

  1. Read a entire line from stdin/file.
  2. Removes any trailing newline.
  3. Places the line, in its pattern buffer.
  4. Modify the pattern buffer according to the supplied commands.
  5. 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

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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 Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 31 comments… read them below or add one }

1 Chuck Gregory September 14, 2009 at 5:54 am

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!

2 NetSpider September 14, 2009 at 7:53 am

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)

3 SamD September 14, 2009 at 2:43 pm

Excellent tutorial.

However, in the very last example, the match is on ‘Design’, not on ‘Website’.

4 ShekarKCB September 14, 2009 at 11:05 pm

Usefull Article, thanks Ramesh…

5 satyavani October 8, 2009 at 5:06 pm

Hi,
Very good tutorial. Really it helped me a lot. Thank u sooooooo much.

6 Berry October 26, 2009 at 6:53 am

Very good tutorial for sed. Thank you very much.

7 Harsh November 15, 2009 at 10:35 pm

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.

8 Sasikala November 16, 2009 at 2:33 am

@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.

9 marble December 10, 2009 at 10:05 am

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

10 Satish February 6, 2010 at 7:00 am

Very helpful!

11 aniketh December 4, 2010 at 10:07 am

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 ?

12 Andrii October 24, 2011 at 1:29 pm

How to find last 10 lines if I don’t know size of file?

13 Soundar November 21, 2011 at 9:16 pm

It’s helping a lot ……thank uuuuuuuuuuuuuu

14 doomhammer65ir November 25, 2011 at 7:06 pm

Hi ramesh
whats wrong with this code?

for ((i=1;i<10;i++))
do
echo $i >> newfile
done

for ((i=1;i<10;i++))
do
sed -n '$i'p newfile
done

=========
I want to use sed for printing a file line by line

15 doomhammer65ir November 27, 2011 at 3:25 pm

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

16 doomhammer65ir November 27, 2011 at 3:27 pm
17 bhavya December 5, 2011 at 2:05 pm

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

18 Hedy Xu January 12, 2012 at 1:48 pm

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

19 Park January 25, 2012 at 6:15 pm

It’s very helpful to understand what sed is!.

20 Rahul Prasad January 28, 2012 at 9:56 am

i want to print the last 5 lines of a file using sed command???

21 shiva March 3, 2012 at 8:42 am

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..?

22 Satheesh Chandra April 29, 2012 at 9:39 am

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

23 Sachin June 15, 2012 at 6:08 am

I just started learning sed and I am glad I found this article. You made it very illustrative with all the examples. Thank you

24 venky February 18, 2013 at 11:46 pm

Hi All,
How to print 3th line first and 2nd line next??
3. Hardware
2. Databases – Oracle, mySQL etc.

25 shweta February 20, 2013 at 11:33 pm

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

26 bharath March 20, 2013 at 7:52 pm

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.

27 Priyanka April 12, 2013 at 3:32 am

Really Awesome!!!

28 Prasanth M January 8, 2014 at 3:26 am

Really useful :) Thanks for the article

29 Raj January 10, 2014 at 1:40 pm

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

30 manoj January 21, 2014 at 4:50 am

sed -n ‘/PATTERN/,+Np’ filename in not working in Solaris? any alternative

31 Ravindra March 1, 2014 at 1:35 am

it helped * me alot.

Leave a Comment

Previous post:

Next post: