【发布时间】:2014-11-05 00:12:16
【问题描述】:
我对三个字符串使用带有 N 选项的 SED,但他错过了最后一个字符串。 如果行数为偶数,则一切正常
示例文本:
this is the first line
this is the second line
this is the third line
sed 'N;s/^.*\(first.*\)\n\(second.*$\)/\1\2/;s/ //g'
我想得到
这是第一行这是第二行
第三条线
但我明白了
这是第一行这是第二行
第三行
最后一行没有被处理, sed 跳过这个下标
s/ //g
四行没问题
this is the first line
this is the second line
this is the third line
this is the fourth line
thisisthefirstline**thisisthesecondline**
thisisthethirdline
thisisthefourthline
对于奇数行并不总是处理最后一行
一个简单的解决方案 - 使用管道
sed 'N;s/^.*\(first.*\)\n\(second.*$\)/\1\2/' | sed 's/ //g'
但我会在没有管道的情况下这样做
【问题讨论】: