Part 3 UNIX Stream Editor: sed
This is the last part on sed. Part 1 and 2 can be found as linked..
Multiple lines in pattern space
Using N for multiple pattern lines
The instruction N appends a newline character to the pattern space and waits for the next line. When the next line is read, it appends the line to the pattern space. Finally the resulting pattern space is output. Check these examples to understand N instruction.
[user9@localhost ~]$ sed 'N'
this is a test
the line is not immediately printed here
this is a test
the line is not immediately printed here
[user9@localhost ~]$ sed 'N'
1
2
1
2
3
4
3
4
[user9@localhost ~]$ sed 'N
> N'
1
2
3
1
2
3
4
5
6
4
5
6
Print (P), delete (D) up to first embedded new line
The instruction P prints the first part of the pattern space up to first embedded newline character and D deletes the first part of the pattern space up to first embedded newline character.
In the following example, the first line is read, N adds a newline character and appends the next line after the new line character. At this point, the pattern space has
[user9@localhost ~]$ sed 'N
P
D'
this is the first line
this is the second line
this is the first line
this is the third line'
this is the second line
this is the fourth line
this is the third line'
this is the fifth line
this is the fourth line
this is the sixth
this is the fifth line
^d
this is the sixth
[user9@localhost ~]$
Temporary buffer
A temporary buffer is available in addition to the pattern space. This additional pattern space can be used to hold data from pattern space using h and retrieve using g. H and G work with the temporary buffer in a manner similar to how N and P work with pattern space. In the following example, each line is read into the pattern buffer. Instruction G copies from temporary buffer (which is empty) and appends to the pattern space. The pattern space is now
[user9@localhost ~]$ sed 'G' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
user20:x:520:520::/home/user20:/bin/bash
user23:x:521:521::/home/user23:/bin/bash
[user9@localhost ~]$
g copies the temporary buffer to pattern space, replacing whatever is in the pattern space and the output is printed. Here g causes the empty temporary buffer to be copied into the pattern space and printed. Only new lines are printed as output.
[user9@localhost ~]$ sed 'g' /etc/passwd
[user9@localhost ~]$
In the following example, instruction 1h reads the first line into the temporary buffer and $G retrieves it at the end (notice the $). The result being that the content of the file is printed to the output followed by the first line.
[user9@localhost ~]$ sed '1h
> $G' /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
user20:x:520:520::/home/user20:/bin/bash
user23:x:521:521::/home/user23:/bin/bash
root:x:0:0:root:/root:/bin/bash
[user9@localhost ~]$
H appends the line in pattern space to the the temporary buffer (as opposed to h which replaces the temporary buffer with pattern space). In this example 1h causes the first line in the pattern space to be copied to the temporary buffer; 5H appends the fifth line to the temporary buffer and $G retrieves the temporary buffer at the end.
[user9@localhost ~]$ sed '1h
> 5H
> $G' /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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
user20:x:520:520::/home/user20:/bin/bash
user23:x:521:521::/home/user23:/bin/bash
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[user9@localhost ~]$
Comments
Post a Comment