Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用bash脚本解析和编辑源代码_Bash_Shell_Parsing - Fatal编程技术网

使用bash脚本解析和编辑源代码

使用bash脚本解析和编辑源代码,bash,shell,parsing,Bash,Shell,Parsing,我确实有几个源代码文件(.cpp),其中包含以下行: other stuff ... TRACE(arg1, arg2, ...); ... other stuff 其中arg1,arg2。。。表示任意函数调用参数,参数之间用逗号分隔,前面可能有空格 目标是解析项目中每个源文件中的每一行,并从函数调用跟踪中删除第一个参数(arg1),例如 TRACE(arg2, arg3, ...); 请注意,函数跟踪前面可以有任意数量的空格,并且函数跟踪本身可以包含字符串跟踪,例如 TRACE(0, TR

我确实有几个源代码文件(.cpp),其中包含以下行:

other stuff
...
TRACE(arg1, arg2, ...);
...
other stuff
其中arg1,arg2。。。表示任意函数调用参数,参数之间用逗号分隔,前面可能有空格

目标是解析项目中每个源文件中的每一行,并从函数调用跟踪中删除第一个参数(arg1),例如

TRACE(arg2, arg3, ...);
请注意,函数跟踪前面可以有任意数量的空格,并且函数跟踪本身可以包含字符串跟踪,例如

TRACE(0, TRACE_INFO, "Test ");
    TRACE(  0 , TRACE, 4,5 ,6);
有没有一个简单的方法

测试文件test.cpp包含:

   TRACE( "I2Ccontrol::I2Ccontrol", TRACE_FATAL,0);

3个空格,没有前几行,没有后几行,使用sed和扩展正则表达式:

sed -r 's/(\bTRACE\()([^",]+,\s*|\s*"[^"]*"\s*,\s*)(.*\);)/\1\3/' file
要就地更改文件,请使用
sed--in-place-r…

编辑:

测试来自注释的输入:

echo '   TRACE( "I2Ccontrol::I2Ccontrol", TRACE_FATAL,0);' |sed -r 's/(\bTRACE\()([^",]+,\s*|\s*"[^"]*"\s*,\s*)(.*\);)/\1\3/'
#three white space before TRACE
输出:

   TRACE(TRACE_FATAL,0);

伟大的此解决方案甚至支持
arg1
值中的逗号。非常感谢!这对多个文件(如:sed--in-place-r。。。文件1文件2?但是现在每个括号后面会不会有一个空格?比如TRACE(arg2,…真的只是编码风格的一致性。不过我还有一个问题:函数调用可能是这样的:TRACE(0,TRACE_INFO,“Test”…这不仅会删除0,而且会删除arg2,在这种情况下TRACE_INFO肯定是一个真正的问题。Test.cpp的内容:TRACE(“I2Ccontrol::I2Ccontrol”,TRACE_FATAL,0);(前面有3个空格)以下命令sed--in-in-place-r's/(\bTRACE()([^“,]+,\s*|\s*“[^”]*,\s*)(*)/\1\3/”test.cpp根本不影响文件test.cpp