Regex 如何使用SED在文本文档中搜索和替换?
我有一个R统计包的脚本,我正试图使用SED修改它。现在看起来是这样的:Regex 如何使用SED在文本文档中搜索和替换?,regex,sed,Regex,Sed,我有一个R统计包的脚本,我正试图使用SED修改它。现在看起来是这样的: foo = [something unimportant] summary(foo) 我希望它看起来像: foo=[something unimportant] print('foo') summary(foo) 我试过: sed 's/summary\((.+)\)/print\(\'\1\'\)\nsummary\(\1\)/' <infile.txt >outfile.txt sed的/summar
foo = [something unimportant]
summary(foo)
我希望它看起来像:
foo=[something unimportant]
print('foo')
summary(foo)
我试过:
sed 's/summary\((.+)\)/print\(\'\1\'\)\nsummary\(\1\)/' <infile.txt >outfile.txt
sed的/summary\(.+)\)/print\(\'\1\'\)\nsummary\(\1\)/'outfile.txt
但这似乎不起作用。我觉得我的regex-fu缺少了。有什么建议吗?我想你在找
sed“s/summary(\(.*))/print('\1')\n\0/”…
。Sed使用posix正则表达式,其中(和)不是特殊字符。相反,组由\(
和\)
分隔
输入:
foo = [something unimportant]
summary(foo)
输出:
foo = [something unimportant]
print('foo')
summary(foo)