Bash 塞德。如何更改特定图案旁边的线条
我的档案是:Bash 塞德。如何更改特定图案旁边的线条,bash,sed,Bash,Sed,我的档案是: DIVIDER Sometext_string many lines of random text DIVIDER Another_Sometext_string many many lines DIVIDER Third_sometext_string .... 如何按照分隔符模式更改线条 结果必须是: DIVIDER [begin]Sometext_string[end] many lines of random
DIVIDER
Sometext_string
many
lines
of random
text
DIVIDER
Another_Sometext_string
many
many
lines
DIVIDER
Third_sometext_string
....
如何按照分隔符模式更改线条
结果必须是:
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]
....
这可能有助于-
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]
更新:
[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]
此版本将处理第一个分隔符后的一个空行
[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
解决方案:)
此操作用于处理分隔符后的第一个空行-
[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
我不太确定,但拥有它不会有什么坏处。我添加了另一个解决方案。我注意到第一个分隔符后面有一个空行。如果是这样的话,sed会在你的第一个除法器后面留下两行空白。很好的解决方案。要删除分隔符后的第一个空行以适应所有此类情况,请尝试以下操作:。。。。;n:a;/^$/!BNs/\n/;ta}