本文介绍了塞德如何更改特定图案旁边的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文件是:
DIVIDER
Sometext_string
many
lines
of random
text
DIVIDER
Another_Sometext_string
many
many
lines
DIVIDER
Third_sometext_string
....
如何按照DIVIDER模式更改行
How change lines following DIVIDER pattern
结果必须为:
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]
更新:
此版本将在第一个DIVIDER之后处理一个空白行.
This version will handle one blank line after the first DIVIDER.
[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
解决方案?:)
There are no other questions right now so that I thought I'd offer an alternate awk
solution if you'd like? :)
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]
这用于处理DIVIDER之后的第一个空白行-
This to handle first blank line after DIVIDER -
[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]
这篇关于塞德如何更改特定图案旁边的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!