可能,這將有助於 -
sed '/DIVIDER/{n;s/.*/[begin]&[end]\n/;}' file1
執行:
[jaypal:~/Temp] cat file1
DIVIDER
Sometext_string
many
lines
of random
text
DIVIDER
Another_Sometext_string
many
many
lines
DIVIDER
Third_sometext_string
[jaypal:~/Temp] sed '/DIVIDER/{n;s/.*/[begin]&[end]\n/;}' file1
DIVIDER
[begin]Sometext_string[end]
many
lines
of random
text
DIVIDER
[begin]Another_Sometext_string[end]
many
many
lines
DIVIDER
[begin]Third_sometext_string[end]
UPDATE:
這個版本將處理第一分後,一個空行。
[jaypal:~/Temp] sed -e '0,/DIVIDER/{n;s/.*/[begin]&[end]/;}' -e '/DIVIDER/{n;s/.*/[begin]&[end]\n/;}' file1
DIVIDER
[begin]Sometext_string[end]
many
lines
of random
text
DIVIDER
[begin]Another_Sometext_string[end]
many
many
lines
DIVIDER
[begin]Third_sometext_string[end]
[jaypal:~/Temp]
更新2:
有沒有其他問題,現在讓我想我會提供一個替代解決方案awk
如果你喜歡? :)
awk '/DIVIDER/{print;getline;sub(/.*/,"[begin]&[end]");print;next}1' file1
[jaypal:~/Temp] awk '/DIVIDER/{print;getline;sub(/.*/,"[begin]&[end]\n");print;next}1' file1
DIVIDER
[begin]Sometext_string[end]
many
lines
of random
text
DIVIDER
[begin]Another_Sometext_string[end]
many
many
lines
DIVIDER
[begin]Third_sometext_string[end]
[jaypal:~/Temp]
這種處理第一個空白分割線後 -
[jaypal:~/Temp] awk '/DIVIDER/{count++;print;getline;if(count==1) sub(/.*/,"[begin]&[end]");else sub(/.*/,"[begin]&[end]\n");print;next}1' file1
DIVIDER
[begin]Sometext_string[end]
many
lines
of random
text
DIVIDER
[begin]Another_Sometext_string[end]
many
many
lines
DIVIDER
[begin]Third_sometext_string[end]
[jaypal:~/Temp]
我不知道,如果這只是我的版本的sed的,但我不得不更換線後使用分號所以它不認爲右括號是一個標誌:'sed'/DIVIDER/{n;s/.*/[begin]&[end]\n/;}'file1' –
嗯,我不太確定,但擁有它不會傷害。我增加了另一個解我注意到第一個DIVIDER後面有一個空行。如果是這種情況,sed會在您的第一個DIVIDER之後創建兩個空行。 –
很好的解決方案。爲了消除分隔符後第一個空行的刪除以滿足所有這些情況,試試這個:'....; n:a;/^ $ /!b; N; s/\ n //; ta}' – potong