我的档案是:
DIVIDER
Sometext_string
many
lines
of random
text
DIVIDER
Another_Sometext_string
many
many
lines
DIVIDER
Third_sometext_string
....
Run Code Online (Sandbox Code Playgroud)
如何在DIVIDER模式后更改行
结果必须是:
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]
....
Run Code Online (Sandbox Code Playgroud)
可能这会有所帮助 -
sed '/DIVIDER/{n;s/.*/[begin]&[end]\n/;}' file1
Run Code Online (Sandbox Code Playgroud)
执行:
[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]
Run Code Online (Sandbox Code Playgroud)
更新:
此版本将在第一个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]
Run Code Online (Sandbox Code Playgroud)
更新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]
Run Code Online (Sandbox Code Playgroud)
这是为了处理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]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3356 次 |
最近记录: |