sed 匹配行不包含带括号的表达式

蝙蝠

(我有遗留的 C+ 代码,该代码使用 gcc 编译时发出警告,并因 clang 错误而失败。方法签名是function(int,(char*),(char*)) or function(int,string,string)。此方法在整个代码中大量使用。)

因此,在尝试使调用正常化时,我尝试使用 sed。假设这个输入:

function(DEFINED_VAR, (char*)someMethod(OTHER_VAR).c_str() + more->stuff, (char*)"Class::method42()");   // 1
function(DEFINED_VAR,(char*)someMethod(OTHER_VAR).c_str() + more->stuff, (char*)"Class::method42()");    // 2
function(DEFINED_VAR, (char *)someMethod(OTHER_VAR).c_str() + more->stuff, "Class::method42()");         // 3
function(DEFINED_VAR, (char*) someMethod(OTHER_VAR).c_str() + more->stuff, "Class::method42()");         // 4
function(DEFINED_VAR, (char *)someMethod(OTHER_VAR).c_str() + more->stuff, (char*)"Class::method42()");  // 5
function(DEFINED_VAR, (char*)someMethod(OTHER_VAR).c_str() + more->stuff, (char*)"Class::method42()");   // 6
function(DEFINED_VAR, (char*)someMethod(OTHER_VAR).c_str() + more->stuff, (char *)"Class::method42()");  // 7
function(DEFINED_VAR, (char*)someMethod(OTHER_VAR).c_str() + more->stuff, ( char* )"Class::method42()"); // 8
function(DEFINED_VAR, (char*)someMethod(OTHER_VAR).c_str() + more->stuff, "Class::method42()");          // 9
function(DEFINED_VAR, (char*)someMethod(OTHER_VAR).c_str() + more->stuff, (char*)"Class::method42()");   // 10
function(DEFINED_VAR, ( char* )someMethod(OTHER_VAR).c_str() + more->stuff, "Class::method42()");        // 11
function(DEFINED_VAR, (char*)someMethod(OTHER_VAR).c_str() + more->stuff, (char*)"Class::method42()");   // 12

我希望第 3、4、9 和 11 行以 (char*)"whateverstring");

到目前为止我尝试过的:

sed -n '/^\s*function\s*(\s*\w.*,\s*(\s*char\s*\*.*,\s*\([^(]\|$\)/p' inputfile.cpp

但我不能完全排除第二个“(char*)”出现以跳过正确的行。

18317221831722很接近(我更喜欢前者),但(我没有找到。

我错过了什么?我在 Ubuntu Xenial 上使用 GNU sed。我对 awk(但我认为它不允许捕获模式匹配)或 perl 很酷,如果这里需要的东西并不容易。

VAR 和“字符串”可能会有所不同,我已经在考虑空格。

端口

使用地址范围,您可以在不匹配两个连续的行上应用替换(char *\*)

sed '/\(char *\*\).*\(char *\*\)/!s/\("[^"]*");\)/(char*)&/' inputfile.cpp

编辑:

要确保替换仅适用于函数定义,只需更改替换模式:

sed '/\(char *\*\).*\(char *\*\)/!s/\(^ *function.*\)\("[^"]*");\)/\1(char*)\2/' inputfile.cpp

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章