2014-11-05 44 views
0

我使用SED与N选项三个字符串,他错过了最后一个字符串。 如果偶数行则一切正常sed多行跳跃行与N选项

示例文本:

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' 

我想

thisisthefirstline thisisthesecondline
thethirdline

,但我得到

thisisthefirstline thisisthesecondline
第三行

最后一行不处理, 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'  

,但我会做没有管

回答

1

因为sed的在看结束它不能N对你,所以它不处理它,只是把最后一行没有N,如:

sed '$!N;s/^.*\(first.*\)\n\(second.*$\)/\1\2/;s/ //g'

$表示最后一行,而!意思不是 - 所以$!N表示N,除了最后一行。

而且我认为你想要的答案的N个问题,但你真的不需要N代表你在做什么,就足够了做:

sed 's/^.*\(first.*\)\n\(second.*$\)/\1\2/;s/ //g'