ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Sed的多行模式学习笔记

2021-12-22 14:03:18  阅读:162  来源: 互联网

标签:多行 Owner 笔记 Operator Sed 模式 空间 line Guide


多行模式空间

sed能查看模式空间的多个行,这就是允许匹配模式扩展到多行上。

多行追加下一行(N)

多行Next(N)命令通过读取新输入行,并将它添加到模式空间的现有内容之后来创建多行模式空间。模式空间最初的内容和新的输入行之间用换行符分隔。在模空间中嵌入的换行符可以利用转义序列"\n"来匹配。在多行模式空间中,元字符"^“匹配空间中的第一个字符,而不匹配换行符后面的字符。”$"只匹配模式空间中最后的换行符,而不匹配任何嵌入的换行符。执行N命令后,控制将被传递给脚本中的后续命令。

Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.

/Operator$/{
N
s/Owner and Operator\nGuide /Installation Guide\	# 末位替换换行符"\"
/
}

result:
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
available on your system.

Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided including the Owner and
Operator Guide and the User Guide.
The Owner and Operator Guide is shipped with your system.

s/Owner and Operator Guide/Installation Guide/
/Owner/{
N
s/ *\n/ /	# 将普通	
s/Owner and Operator Guide */Installation Guide\
/
}

result:
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
available on your system.
Look in the Installation Guide shipped with your system.
Two manuals are provided including the Installation Guide
and the User Guide.
The Installation Guide is shipped with your system.
多行删除(D)

删除模式空间中直到第一个嵌入的换行符的这部分内容。它不会导致读入新的输入行,相反,它返回到脚本的顶端,将这些指令应用于模式空间剩余的内容。

 This line is followed by 1 line.

 This line is followed by 2 line.


 This line is followed by 3 line.



 This line is followed by 4 line.




 This is the end.

# 删除多余空格,仅保留一行空格。
/^$/{
N
/^\
$/D
}

result:

 This line is followed by 1 line.

 This line is followed by 2 line.

 This line is followed by 3 line.

 This line is followed by 4 line.

 This is the end.


多行打印§

该命令输出多行模式空间的第一部分,直到第一个嵌入的换行符为止。在执行完脚本的最后一个命令之后,模式空间的内容自动输出(-n选项或#n抑制这个默认动作)。因此,当默认的输出被抑制或脚本中的控制流更改,以至不能到达脚本的底部时,需要使用打印命令(P或p).Print命令经常出现在Next命令之后和Delete命令之前。这三个命令能建立一个输入/输出循环,用来维护两行的模式空间,但一次只输出一行。这个循环的目的是只输出模式空间的第一行,然后返回到脚本的顶端将所有的命令应用于模式空间的第二行。没有这个 循环,当执行脚本中的最后一个命令时,模式空间中的这两行将被输出。

Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
Operating System.

/UNIX$/{
N	# 将一行新的输入行加入模式空间
/\
System/{
s// Operating &/
P	# 替换命令应用于多行模式空间之后,模式空间的第一部分被Print命令输出,然后被Delete命令删除。
D	# 阻止脚本到达底部,输出两行并清除模式空间的内容。Delete命令保护了模式空间的第二部分,并将控制转移脚本顶端。顶端所有的编辑命令都可以被应用于一行。
}
}

result:
Here are examples of the UNIX Operating
System. Where UNIX Operating
System appears, it should be the UNIX
Operating System.

N、P、D的联合使用,是为了一旦进行跨越两行的替换,就打印第一行并且把它从模式空间删除。第二部分保留在模式空间中,将控制转移到脚本的顶端,这时检查是否在该行上还有其他的匹配字符。

标签:多行,Owner,笔记,Operator,Sed,模式,空间,line,Guide
来源: https://blog.csdn.net/weixin_45096752/article/details/122084336

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有