我想知道是否可以在第一次使用gnu sed的搜索字符串中插入文本文件中间的文本。
因此,例如,如果搜索字符串为" Hello",我想在新行上首次出现" Hello"后立即插入字符串
Hello John, How are you?
....
....
....
Hello Mary, How are you doing?
字符串将在"你好约翰,你好吗?"之后输入。在新线上
谢谢,
您可以说:
sed '/Hello/{s/.*/&nSomething on the next line/;:a;n;ba}' filename
为了在第一次出现所需字符串之后插入一条线,例如Hello
与您的问题一样。
对于您的示例数据,它会产生:
Hello John, How are you?
Something on the next line
....
....
....
Hello Mary, How are you doing?
使用sed:
sed '/Hello John, How are you?/s/$/nsome stringn/' file
Hello John, How are you?
some string
....
....
....
Hello Mary, How are you doing?
使用 awk
awk '/Hello/ && !f {print $0 "nNew line";f=1;next}1' file
Hello John, How are you?
New line
....
....
....
Hello Mary, How are you doing?
此搜索sting Hello
,如果标志f
不正确(默认为start)
如果这是真的,请打印行,打印额外的文本,将标志f
设置为true,跳到下一行。
下次发现Hello
时,FLAG f
是正确的,什么也不会做。
sed '/Hello/ a/
Your message with /
New line' YourFile
a/
for Append(之后)您的模式, i/
for instert(之前)