开发者

How do I search and replace in a text document using SED?

开发者 https://www.devze.com 2023-03-28 03:01 出处:网络
I have a script for the R statistical package that I开发者_如何学编程\'m trying to modify using SED. Right now, it looks like this:

I have a script for the R statistical package that I开发者_如何学编程'm trying to modify using SED. Right now, it looks like this:

foo = [something unimportant]
summary(foo)

I'd like it to look like:

foo=[something unimportant]
print('foo')
summary(foo)

I've tried:

sed 's/summary\((.+)\)/print\(\'\1\'\)\nsummary\(\1\)/' <infile.txt >outfile.txt 

but that seems to not work. I have the feeling my regex-fu is lacking. Any suggestions?


I think you're looking for sed "s/summary(\(.*\))/print('\1')\n\0/" .... Sed uses posix regex, where ( and ) aren't special characters. Instead, groups are delimited by \( and \).

Input:

foo = [something unimportant]
summary(foo)

Output:

foo = [something unimportant]
print('foo')
summary(foo)
0

精彩评论

暂无评论...
验证码 换一张
取 消