Unix Sed Tutorial8 : Multi-Line File Operation with 6 Practical Examples


As part of our on going UNIX sed tutorial series 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 our first sed tutorial ?. 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.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 ).

Sed Example 1. Join Two Consecutive Lines

$ sed -e '{
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.

In the above example,

  • 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 ” @ “
大括号用来组合命令.每条命令必须分行写. (当然按照bash的惯例,可以用分号;来代替分行)
sed读取第一行,将它放入模式空间.N读取下一行追加到模式空间,并用\n隔开 . 现在的模式空间中是 第一行\n第二行
下一个命令是将\n替换成@空格
sed的默认动作是将模式空间中的打印出来.得到上述的结果

Sed Example 2. Number each non-blank line of a file

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 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.
第一句打印出行号和原文  (自己执行第一句试试)
第二句行号和原文间的\n替换成空格了

Sed Example 3. Delete Two Consecutive Blank Lines from Input

$ 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’.

如果读取到空行,则读取下一行. 如果下一行也是空行,则模式空间中此时是 ^\n$

第二句如果匹配^\n$,则删除该行,开始下一轮循环d(读取新的一行到模式空间,并执行命令)


Sed Example 4. Delete Last 2 Lines of a file

Before viewing this example you must aware of two interesting sed command.

  1. P – which prints the first line of a pattern space. (till first \n). 打印第一行(到\n结束)
  2. D – Delete first line from the pattern space. Control then passes to the top of the script.(删除第一行(到\n结束),并且忽略后面的命令,开始执行第一条命令 )
$ 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
  1. Reads the first line and place it in the pattern space.
  2. N command reads the next line and append to pattern space seperated by newline. (Now firstline\nsecond line)
  3. 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.
  4. Like this it joins 2nd\n3rd lines, 3rd\n4th lines and goes on.
  5. 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 $).

1 读取第一行放入模式空间

2 N读取下一行,并追加到模式空间,用\n隔开
3 如果不是最后一行($), 打印第一行(P),删除第一行开始下轮循环(D) (所谓循环是 继续在模式空间 开始执行第一条命令. 注意:不读取新的一行. )
5 如果是最后一行 删除此行 (就一下子删了2行)

Sed Example 5. Print Last 2 Lines of a file

$ sed '$!N;$!D' thegeekstuff.txt
Website Design
Windows- 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.


Sed Example 6. Delete Duplicate, Consecutive Lines from a file 删除连续的行中重复的行

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值