skip to main content

SAP NetWeaver Newbie

Part 2 UNIX Stream Editor: sed

This is a continuation of  Part 1 UNIX Stream Editor: sed.

Print instruction p

The instruction p prints the pattern space. In the following example, the lines are printed twice. Once by sed and once by the instruction p.

[user9@localhost ~]$ sed 'p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin

No print option -n
In the above example, the lines were printed twice. You can use -n (no print) option to suppress the print by sed. The output is printed only if there is an instruction  to print (instruction p in this case)

[user9@localhost ~]$ sed -n 'p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

Pattern matching
Let's use -n and p to do a pattern match. You can use context addressing to find the pattern and instruction p to print the pattern space to achieve that. The result is similar to what is achieved by grep.

[user9@localhost ~]$ sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

To print only those lines that begin with root:

[user9@localhost ~]$ sed -n '/^root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash

Head and tail
You can achieve results similar to those achieved by head command. The following example shows how first 4 line are printed. You have to remember that sed will read all the input lines into the pattern space and apply the instruction. Therefore in this example all the input lines are read into the pattern space and the first 4 lines are printed.

[user9@localhost ~]$ sed -n '1,4p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

The following example shows how 50th line to last line is printed.

[user9@localhost ~]$ sed -n '50,$p' /etc/passwd
user16:x:516:516::/home/user16:/bin/bash
user17:x:517:517::/home/user17:/bin/bash
appli:x:518:518::/home/appli:/bin/bash
user19:x:519:519::/home/user19:/bin/bash
user20:x:520:520::/home/user20:/bin/bash
user23:x:521:521::/home/user23:/bin/bash

Printing line number
The instruction = prints the line number to the output. Notice that in this example -n option. The example results in printing the line number followed by the line.

[user9@localhost ~]$ sed '=' /etc/passwd
1
root:x:0:0:root:/root:/bin/bash
2
bin:x:1:1:bin:/bin:/sbin/nologin
3
daemon:x:2:2:daemon:/sbin:/sbin/nologin

You can find the number of lines in a file using sed by printing the last line's line number.

[user9@localhost ~]$ sed -n '$=' /etc/passwd
55

The following example uses context addressing to print the line numbers of the line matching a pattern.

[user9@localhost ~]$ sed -n '/root/=' /etc/passwd
1
12

Quit instruction q
In the example where we emulated head command using option p, we explained how all the lines are read and only the first few lines were printed according to the instruction passed. The instruction q can be used to quit reading the lines when a context or line address is reached. The following example prints each line that is read into the buffer and quits after the 4th line.

[user9@localhost ~]$ sed '4q' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

[user9@localhost ~]$ sed 'q' /etc/passwd
root:x:0:0:root:/root:/bin/bash

Write output to file with w instruction
The following example shows how the output can be witten to a file using w.

[user9@localhost ~]$ sed 'w out'
this is the first line
this is the first line
this is the second line
this is the second line
thi is end
thi is end
[user9@localhost ~]$ cat out
this is the first line
this is the second line
thi is end

Append text to pattern space with instruction r
Using r, you can read a file and append its content to the pattern space.

[user9@localhost ~]$ cat > test
++
---
****
[user9@localhost ~]$ sed 'r test'
Im typing something here and r reads the contents in pattern buffer
Im typing something here and r reads the contents in pattern buffer
++
---
****

Here is an example with which one can append file content to the last line

[user9@localhost ~]$ sed '$r test' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
user23:x:521:521::/home/user23:/bin/bash
++
---
****

Inserting, appending, replacing and deleting pattern space
Using instructions i, a, c and d, you can insert a line before the pattern space, append line to the pattern space, replace pattern space with a line and delete the pattern space respectively.

Inserting lines:
[user9@localhost ~]$ sed 'i\
> This is added before the line' /etc/passwd
This is added before the line
root:x:0:0:root:/root:/bin/bash
This is added before the line
user23:x:521:521::/home/user23:/bin/bash

Appending line
[user9@localhost ~]$ sed 'a\
> ---' /etc/passwd
root:x:0:0:root:/root:/bin/bash
---
user20:x:520:520::/home/user20:/bin/bash
---
user23:x:521:521::/home/user23:/bin/bash
---

Replacing line
[user9@localhost ~]$ sed 'c\
> this will replace the entire line with a new text' /etc/passwd
this will replace the entire line with a new text
this will replace the entire line with a new text
this will replace the entire line with a new text
this will replace the entire line with a new text

Delete the contets of pattern space
[user9@localhost ~]$ sed 'd' /etc/passwd
[user9@localhost ~]$

1 comment:

Email Subscription

Get every new post into your inbox by subscribing us.

Want a reason to subscribe?
1. This sitemap might convince you to subscribe.
2. We do not misuse email IDs. We respect privacy.

© 2008 - 2017 sapnwnewbie. All rights reserved.