【发布时间】:2020-12-13 22:33:37
【问题描述】:
(我有旧版 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 *)”出现以跳过正确的行。
1831722 和 1831722 很接近(我更喜欢前者),但 ( 让我望而却步。
我错过了什么?我在 Ubuntu Xenial 上使用 GNU sed。我很喜欢 awk(但我认为它不允许捕获模式匹配)或 perl,如果这里需要的东西并不容易。
VAR 和“字符串”可能会有所不同,我已经考虑了空格。
【问题讨论】: