本文共 5749 字,大约阅读时间需要 19 分钟。
As part of our on going earlier we covered the printing, deletion, substitution, file write, file manipulation commands etc., with the single line in the pattern space.
In this article let us review how to do the multi-line operation in Sed.
Do you remember the Sed working methodology which we learned in ?. In that article we explained that 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.
在那一章中,我们解释了sed读取一行,删掉后面的换行符,将这行放进模式空间,执行每一条命令,再打印出模式空间.
In 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.
sed从输入读取下一行,将下一行添加到模式空间.下一行与原来的行用换行符\n隔开.
Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.
$ cat thegeekstuff.txtLinux SysadminDatabases - Oracle, mySQL etc.Databases - Oracle, mySQL etc.Security (Firewall, Network, Online Security etc)Storage in LinuxWebsite DesignWebsite DesignWindows- Sysadmin, reboot etc.$
Note: There are two consecutive blank lines in the above input. ( 5th and 6th line ).
$ sed -e '{Ns/\n/ @ /}' thegeekstuff.txtLinux Sysadmin @ Databases - Oracle, mySQL etc.Databases - Oracle, mySQL etc. @ Security (Firewall, Network, Online Security etc) @Storage in Linux @ Website DesignWebsite Design @ Windows- Sysadmin, reboot etc.
In the above example,
As mentioned in our previous article, = is a command to get a line number of a file.
$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'1 Linux Sysadmin2 Databases - Oracle, mySQL etc.3 Databases - Oracle, mySQL etc.4 Security (Firewall, Network, Online Security etc)7 Storage in Linux8 Website Design9 Website Design10 Windows- Sysadmin, reboot etc.
$ sed '/^$/{N/^\n$/d}' thegeekstuff.txtLinux SysadminDatabases - Oracle, mySQL etc.Databases - Oracle, mySQL etc.Security (Firewall, Network, Online Security etc)Storage in LinuxWebsite DesignWebsite DesignWindows- 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’.
如果读取到空行,则读取下一行. 如果下一行也是空行,则模式空间中此时是 ^\n$
第二句如果匹配^\n$,则删除该行,开始下一轮循环d(读取新的一行到模式空间,并执行命令)
Before viewing this example you must aware of two interesting sed command.
$ sed 'N;$!P;$!D;$d' thegeekstuff.txtLinux SysadminDatabases - Oracle, mySQL etc.Databases - Oracle, mySQL etc.Security (Firewall, Network, Online Security etc)Storage in LinuxWebsite Design
$ sed '$!N;$!D' thegeekstuff.txtWebsite DesignWindows- Sysadmin, reboot etc.
The above joins and deletes each line until last two lines are there in the pattern space. When it reaches $, prints the pattern space which will have only last two lines.
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.txtLinux SysadminDatabases - Oracle, mySQL etc.Security (Firewall, Network, Online Security etc)Storage in LinuxWebsite DesignWindows- Sysadmin, reboot etc.
转载地址:http://fyffb.baihongyu.com/