1

我正在尝试更改这样的文件:

Hello
My name is [Bob]
My age is 34

应该变成:

Hello
My name is [Bob]SomeText[Bob]SomeText
My age is 34

但是,为了更容易,我知道我正在寻找的“字符串”总是以 [Bo

任何帮助将不胜感激,如果您需要更多信息,请说:)

4

3 回答 3

0
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
> 
于 2013-05-09T11:42:32.897 回答
0

如果你真的不需要bash只使用,你可以使用sed

 sed 's/\[Bo.*/&SomeText&SomeText/g' filename

输出:

Hello
My name is [Bob]SomeText[Bob]SomeText
My age is 34
于 2013-05-09T11:19:52.400 回答
0

使用sed. 将所有内容匹配[Bo到第一个]并替换它:

sed 's/\(\[Bo[^\]]*\]\)/\1SomeText\1/'
于 2013-05-09T11:21:25.440 回答