我试图找出是否有一种方法可以使用 SED 或 AWK 在嵌套的某些条件下替换单词。示例代码:

void className::functName1_testName1_paramA()
{
     some_code;
    /* comment */
    returnType_1 paramA  = value1;
    returnType_2 paramB  = value2;
    returnType_3 paramC  = value3;
}

void className::functName1_testName2_paramB()
{
    some_code;
    /* comment */
    returnType_1 paramA  = value1;
    returnType_2 paramB  = value2;
    returnType_3 paramC  = value3;
}

void className::functName2_testName1_paramA()
{
    some_code;
    /* comment */
    returnType_1 paramA  = value1;
    returnType_2 paramB  = value2;
    returnType_3 paramC  = value3;
    returnType_4 paramD  = value4;
}

我想要实现的是
IF (functName1 && testName1 && paramA are found/matched) THEN
   IF (returnType_1 && paramA are found/matched in nth line below above match) THEN
      replace value1 with actualValue1;

IF (functName1 && testName2 && paramB are found/matched) THEN
   IF (returnType_2 && paramB are found/matched in nth line below above match) THEN
      replace value2 with actualValue2;

IF (functName2 && testName1 && paramA are found/matched) THEN
   IF (returnType_1 && paramA are found/matched in nth line below above match) THEN
      replace value1 with actualValue1;

等等等等。

我有许多不同functNames、testNames、params 和returnTypes 组合的模板,所以我希望能够像上面那样匹配它。

我想过使用
sed '/functName1/ { n; n; n; s/value1/actualValue1/; }' fileName

但这行不通,因为有很多不同的情况,并且第 n 行在不同的功能之间有所不同。因此,当我在寻找答案时,似乎人们在说 awksed 更好,但我真的找不到与我的案例完全相似的东西。

我假设有办法做到这一点,但我不太确定。所以我想问题是,使用 awksed 甚至可以实现这样的事情吗?

我不介意多次编写 awk/sed 命令,所以如果有人能给我一个例子和这个 awk/sed 命令用法的语法,我可以应用这个概念并编写其余的。

我非常感谢您的时间,并提前感谢您。

预期输出将类似于
void className::functName1_testName1_paramA()
{
     some_code;
    /* comment */
    returnType_1 paramA  = actualValue1;  //after being replaced
    returnType_2 paramB  = value2;
    returnType_3 paramC  = value3;
}

void className::functName1_testName2_paramB()
{
    some_code;
    /* comment */
    returnType_1 paramA  = value1;
    returnType_2 paramB  = actualValue2;  //after being replaced
    returnType_3 paramC  = value3;
}

void className::functName2_testName1_paramA()
{
    some_code;
    /* comment */
    returnType_1 paramA  = actualValue1; //after being replaced
    returnType_2 paramB  = value2;
    returnType_3 paramC  = value3;
    returnType_4 paramD  = value4;
}

最佳答案

您能否尝试以下操作,完全基于显示的示例。

awk '
!NF{
  found_func2_test1_parama=found_func2_test1_paramb=found_func1_test1_parama=""
}
/functName1_testName1_paramA/{
  found_func1_test1_parama=1
}
/returnType_1 paramA/ && found_func1_test1_parama{
  $NF="actualValue1;"
  sub(/returnType/,"    &")
  print
  next
}
/functName2_testName1_paramB/{
  found_func2_test1_paramb=1
}
found_func2_test1_paramb && /returnType_2 paramB/{
  $NF="actualValue2;"
  sub(/returnType/,"    &")
  print
  next
}
/functName2_testName1_paramA/{
  found_func2_test1_parama=1
}
found_func2_test1_parama && /returnType_1 paramA/{
  $NF="actualValue1;"
  sub(/returnType/,"    &")
  print
  next
}
1
'   Input_file

关于bash - 如何在不同行的多个模式匹配后替换第 n 行的单词?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58436084/

10-16 21:42