sed系列:文件多行操作

Sed reads line by line, removes any trailing new lines, places a line in a pattern space buffer, process as per the given commands and prints the pattern space.
n case, if you want to delete all the newlines in a file, you cannot use the following method. Because newline is already removed and placed in the pattern space.
$ sed 's/\n//' filename or  $sed 's/\n/ENDOFLINE\n/' filename
For situations like this sed multi-line is appropriate. Sed provides the command “N” for Multi-Line Operations.
N command reads a next line from the input, Append next line to pattern space. Next line is separated from the original pattern space by a newline character.
先让我们建立测试用例
$cat thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)


Storage in Linux
Website Design
Website Design
Windows- Sysadmin, reboot etc.
Note: There are two consecutive blank lines in the above input. ( 5th and 6th line ).
例1:用@符号连接相连两行
$sed '{
N
s/\n/ @ /
}' thegeekstuff.txt
Linux Sysadmin @ Databases - Oracle, mySQL etc.
Databases - Oracle, mySQL etc. @ Security (Firewall, Network, Online Security etc)
@
Storage in Linux @ Website Design
Website Design @ Windows- Sysadmin, reboot etc.
$
The curly braces “{” and “}” used to group the commands. The curly braces and sed commands must be on the seperate lines.
Sed reads the first line and place it in the pattern space, N command reads the next line and appends with the pattern space i.e first line seperated by newline. So now pattern space will have firstline\nsecondline.
Next substitution of \n to space@space and it prints the pattern space content as its sed default behaviour. So consecutive lines are joined and delimited by ” @ “
例2:显示非空行行号
= is a command to get a line number of a file.
$sed '/./=' thegeekstuff.txt
1
Linux Sysadmin
2
Databases - Oracle, mySQL etc.
3
Databases - Oracle, mySQL etc.
4
Security (Firewall, Network, Online Security etc)


7
Storage in Linux
8
Website Design
9
Website Design
10
Windows- Sysadmin, reboot etc.
$sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'
1 Linux Sysadmin
2 Databases - Oracle, mySQL etc.
3 Databases - Oracle, mySQL etc.
4 Security (Firewall, Network, Online Security etc)

7 Storage in Linux
8 Website Design
9 Website Design
10 Windows- Sysadmin, reboot etc.
$
The first sed command prints the line number and prints the original line in a next line if it is not a blank.( Execute it and see the output of the first sed command ).
Next sed command is just appends pair of lines.
例3:删除相邻的两个空行
$sed '/^$/{
> N
> /^\n$/d
> }' thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Website Design
Website Design
Windows- Sysadmin, reboot etc.
$
If the line is blank, read and appends the next line, /^\n$/ represents, two lines are empty,\n is added by N command. Then just delete the pattern space and start the next cycle using command ‘d’.
例4:删除文件最后两行
Before viewing this example you must aware of two interesting sed command.
P – which prints the first line of a pattern space. (till first \n).
D – Delete first line from the pattern space. Control then passes to the top of the script.
$ sed 'N;$!P;$!D;$d' thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)

Storage in Linux
Website Design

Reads the first line and place it in the pattern space.
N command reads the next line and append to pattern space seperated by newline. (Now firstline\nsecond line)
If it not reaches the last line($), print the first line and delete the first line alone from the pattern space. Then cycle starts again.
Like this it joins 2nd\n3rd lines, 3rd\n4th lines and goes on.
Atlast when it has 9th\n10th line in a pattern space, it reaches $ so it just deletes the pattern space. ($!P and $!D wont print and delete if it is $).

例5:输出文件最后两行
$ sed '$!N;$!D' thegeekstuff.txt
Website Design
Windows- Sysadmin, reboot etc.

例6:删除相邻的两个相同行
The below command checks each line joined with the next line, check if both are same then it doesn’t the print pattern space(!P), just delete the first line from the pattern space. So only one line will be remaining in the pattern space.
$ sed '$!N; /^\(.*\)\n\1$/!P; D' thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)

Storage in Linux
Website Design
Windows- Sysadmin, reboot etc.
$

猜你喜欢

转载自ilnba.iteye.com/blog/1568015