2014-11-05 40 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'