是否可以使用perl从函数定义中删除参数?例如,如果我的文件包含以下文字:
var1 = myfunc(sss,'ROW DRILL',1,1,0);
var2 = myfunc(fff,'COL DRILL',1,1,0);
var3 = myfunc(anyAlphaNum123,'anyAlphaNum123 or space',1,1,0);
donotcapture=myfunc2(rr,'some string',1,1,0);
我需要改变它以便它变成:
var1 = myfunc(sss,'ROW DRILL');
var2 = myfunc(fff,'COL DRILL');
var3 = myfunc(anyAlphaNum123,'anyAlphaNum123 or space');
donotcapture=myfunc2(rr,'some string',1,1,0);
基本上只是从调用,1,1,0
的所有实例中删除myfunc
,但保留前两个参数。
我尝试了以下方法,但这种方法意味着我必须为每个排列编写规则......
perl -pi -w -e "s/myfunc\(rr,'COL SUBSET',1,1,0\)/myfunc\(rr,'COL SUBSET'\)/g;" *.txt
答案 0 :(得分:2)
(
与,
之间的任何内容的正则表达式,,
除外:\(([^,]+),
'
与'
之间的任何内容的正则表达式,'
除外:\'([^']+)\'
(...)
它们填充变量,您可以在替换中将其用作$1
。notmyfunc()
,\b
。\x27
是单引号
\'
- > \x27
(myfunc\([^,]+,\x27[^\x27]+\x27)
;
,这是单个语句不需要的。.
,假设您实际上是这样的。工作代码
(比较聊天记录\((
;反斜杠丢失了,我相信聊天吃掉了。):
perl -pi -w -e "s/(\bmyfunc)\(([^,]+),\'([^']+)\'(?:,\d+){3}\)/\$1\(\$2,\'\$3\'\)/g;" *txt
Ikegamis很好编辑
(在我们的聊天中非常耗时的细节不再容易看到,
因为捕获组的(
被移动到其他地方。):
perl -i -wpe's/\b(myfunc\([^,]+,\x27[^\x27]+\x27)(?:,\d+){3}\)/$1)/g' *.txt
输入:
var1 = myfunc(sss,'ROW DRILL',1,1,0);
var2 = myfunc(fff,'COL DRILL',1,1,0);
var3 = myfunc(s,'ROW SUBSET',1,1,0);
var4 = myfunc(rr,'COL SUBSET',1,1,0);
var5 = myfunc(rr,'COL SUBSET',2,12,50); with different values
var6 = notmyfunc(rr,'COL SUBSET',1,1,0); tricky differet name
var1 = myfunc(sss,'ROW DRILL',1,1,0);
var2 = myfunc(fff,'COL DRILL',1,1,0);
var3 = myfunc(anyAlphaNum123,'anyAlphaNum123 or space',1,1,0);
donotcapture=myfunc2(rr,'some string',1,1,0);
输出(版本“更放松”):
var1 = myfunc(sss,'ROW DRILL');
var2 = myfunc(fff,'COL DRILL');
var3 = myfunc(s,'ROW SUBSET');
var4 = myfunc(rr,'COL SUBSET');
var5 = myfunc(rr,'COL SUBSET'); with different values
var6 = notmyfunc(rr,'COL SUBSET',1,1,0); tricky differet name
var1 = myfunc(sss,'ROW DRILL');
var2 = myfunc(fff,'COL DRILL');
var3 = myfunc(anyAlphaNum123,'anyAlphaNum123 or space');
donotcapture=myfunc2(rr,'some string',1,1,0);
经验教训:
\
。