当前位置: 技术问答>linux和unix
关于sed 高级编程的一些疑问
来源: 互联网 发布时间:2016-08-07
本文导语: 文件:data 的内容是: The first meeting is the Linux System Administrator's group will be held on Tudesday. All System Administrators should attend this meeting. 我执行命令:sed 'N;p' data 的结果是: The first meeting is the Linux ...
文件:data 的内容是:
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
All System Administrators should attend this meeting.
我执行命令:sed 'N;p' data 的结果是:
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
All System Administrators should attend this meeting.
这个结果我是这么理解的,首先N,总是在执行sed编辑器命令之前将下一行文本读入到模式空间中,在达到最后一行时,因为没有下一行可以读入,所以N命令导致sed编辑器停止,最后一行数据没有被读入到模式空间中,所以结果是:
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
然后p命令打印原始的数据内容:
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
All System Administrators should attend this meeting.
执行:sed 'p;N' data 的结果是:
The first meeting is the Linux System
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
All System Administrators should attend this meeting.
All System Administrators should attend this meeting.
以上结果是怎么得来的 不是很清楚,能否有经验的人士给我详细讲一下这个执行及得到结果的过程。
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
All System Administrators should attend this meeting.
我执行命令:sed 'N;p' data 的结果是:
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
All System Administrators should attend this meeting.
这个结果我是这么理解的,首先N,总是在执行sed编辑器命令之前将下一行文本读入到模式空间中,在达到最后一行时,因为没有下一行可以读入,所以N命令导致sed编辑器停止,最后一行数据没有被读入到模式空间中,所以结果是:
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
然后p命令打印原始的数据内容:
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
All System Administrators should attend this meeting.
执行:sed 'p;N' data 的结果是:
The first meeting is the Linux System
The first meeting is the Linux System
Administrator's group will be held on Tudesday.
All System Administrators should attend this meeting.
All System Administrators should attend this meeting.
以上结果是怎么得来的 不是很清楚,能否有经验的人士给我详细讲一下这个执行及得到结果的过程。
|
sed -n 'n;p' file是打印偶数行,sed -n 'p;n' file是打印奇数行.
现在你没有加-n参数,所以sed会先把这一行打出来,再分析是这一行奇数行还是偶数行决定是否再打一次
所以sed 'p;N' data做如下分析
The first meeting is the Linux System
现在你没有加-n参数,所以sed会先把这一行打出来,再分析是这一行奇数行还是偶数行决定是否再打一次
所以sed 'p;N' data做如下分析
The first meeting is the Linux System