我正在尝试更改这样的文件:
Hello
My name is [Bob]
My age is 34
应该变成:
Hello
My name is [Bob]SomeText[Bob]SomeText
My age is 34
但是,为了更容易,我知道我正在寻找的“字符串”总是以
[Bo
任何帮助将不胜感激,如果您需要更多信息,请说:)
perl -pe 'm/(\[Bo.*?\])/g;$a=$1;s/(.*\[Bo.*?\])/$1sometext${a}sometext/g;' your_file
如果您的 [Bo.*] 将始终出现在行尾,则上述解决方案有效。测试:
> cat temp
Hello
My name is [Bob]
My age is 34
> perl -pe 'm/(\[Bo.*?\])/g;$a=$1;s/(.*\[Bo.*?\])/$1sometext${a}sometext/g;' temp
Hello
My name is [Bob]sometext[Bob]sometext
My age is 34
>
如果你真的不需要bash
只使用,你可以使用sed
:
sed 's/\[Bo.*/&SomeText&SomeText/g' filename
输出:
Hello
My name is [Bob]SomeText[Bob]SomeText
My age is 34
使用sed
. 将所有内容匹配[Bo
到第一个]
并替换它:
sed 's/\(\[Bo[^\]]*\]\)/\1SomeText\1/'