Line Addressing in sed

by mike on July 29, 2011

Line Addressing
Line addressing allows you to select the lines that you want to work with.  Addressing can use regular expressions to determine the lines or list numbers for the lines in the format of a number.  If two numbers are used separated by a comma then it becomes a range.

A good example of this is when you place the contents of top into a file so you can work with it.  However, top has five lines at the top that make it hard to work with the lines below.  You can see that in the example.

head top.txt
top - 03:56:59 up  1:39,  1 user,  load average: 0.00, 0.00, 0.00
Tasks:  61 total,   1 running,  60 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.2%us,  0.6%sy,  0.0%ni, 98.7%id,  0.3%wa,  0.1%hi,  0.0%si,  0.0%st
Mem:    421456k total,   238852k used,   182604k free,    24128k buffers
Swap:   538168k total,        0k used,   538168k free,   143408k cached

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
1 root      15   0 10348  680  572 S  0.0  0.2   0:00.86 init
2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/0
3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0

You can remove those lines using addressing.  In this example sed is used to delete, “d” , the first five lines of the text file that has been created.  Then sed is used to print those lines that match the pattern “sshd”.  Note that the first command is run and the output is piped into the second sed instance.

sed '1,5d' top.txt | sed -n '/sshd/p'
1869 root      18   0 62608 1216  652 S  0.0  0.3   0:00.00 sshd
2106 root      15   0 90108 3356 2620 S  0.0  0.8   0:00.50 sshd

Here is an example of printing all lines from line 60 to the end of the file “$”.

sed  '60,$p' processes
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.2   2160   660 ?        Ss   Jul26   0:06 init [3]
dovecot   3314  0.0  0.6   4996  1824 ?        S    Jul26   0:08 imap-login

This command addresses lines 1 through 400 and changes the string “pop3″ to “POP3″ from the processes file and prints the output in a additional file.

sed -n '1,400 s/pop3/POP3/p' processes > newfile
[root@bash ~]# cat newfile
dovecot  16030  0.0  0.6   4992  1820 ?        S    Jul26   0:08 POP3-login
dovecot  16031  0.0  0.6   4992  1820 ?        S    Jul26   0:08 POP3-login
dovecot  16032  0.0  0.6   4992  1820 ?        S    Jul26   0:08 POP3-login

{ 4 comments }

Tim Chase July 29, 2011 at 10:44 am

Here is an example of printing all lines from line 60 to the end of the file “$”.

sed ’60,$p’ processes

Just for the record, that should be “sed -n ’60,$p’ processes”.

This command addresses lines 1 through 400 and changes the string “pop3″ to “POP3″ from the processes file and prints the output in a additional file.

sed -n ’1,400 s/pop3/POP3/p’ processes > newfile

For this one, if the file is long, it’s more efficient to add “401q” as a second sed command so it doesn’t continue processing through lines it doesn’t need to.

anonymous July 30, 2011 at 2:28 am

The line reference can also be a “regular expression” which means the first line containing the regular expression.

Create a data file for this example:
for f in {1..40}; do echo “line ${f}” ; done > /tmp/testing.d8a
Which creates the file:
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20
line 21
line 22
line 23
line 24
line 25
line 26
line 27
line 28
line 29
line 30
line 31
line 32
line 33
line 34
line 35
line 36
line 37
line 38
line 39
line 40

The command:
sed -n ‘/line 4$/,/line .3$/ p’ /tmp/testing.d8a
returns:
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13

Fred July 30, 2011 at 4:32 pm

Keep it simple, use the appropriate commands:

head -400 file, will print the first 400 lines of a file

tail +6 file, will start printing line 6 through the end of file

sure you can do a lot of things with sed, but is it the best tool for the job

vimax July 30, 2011 at 9:53 pm

Thank you for the wise critique. Me & my neighbour were preparing to do some research about that. We received a great book on that matter from our local library and most books where not as influensive as your information and facts. Im really glad to see this kind of facts which I was searching for a long time.
Thanksssss.2011

Comments on this entry are closed.

Previous post:

Next post: