Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a word on nth line after multiple pattern matches that are on different lines?

Tags:

bash

sed

awk

I'm trying to find out if there is a way I can use SED or AWK to replace a word under nested certain conditions. Example code:

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;
}

What I'm trying to achieve is

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;

So on and so forth.

I have templates with a number of combinations of different functNames, testNames, params, and returnTypes so that's what I'd like to be able to match it like the above.

I've thought about using

sed '/functName1/ { n; n; n; s/value1/actualValue1/; }' fileName

But it wouldn't work because there are so many different cases and the nth lines differ among different functions. So when I was searching for answers, it seemed like people were saying awk would be better than sed for stuff like this, but I couldn't really find something that's even remotely similar to my case.

I'm assuming there's a way to do it but I'm not so sure. So I guess the question is, is something like this even possible with awk or sed?

I don't mind writing the awk/sed command multiple times so if someone can give me an example and the syntax of this awk/sed command usage, I could just apply the concept and write the rest.

I really appreciate your time and thank you in advance.

Expected output would be something like

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;
}
like image 271
Sweetfinish Avatar asked Dec 11 '25 02:12

Sweetfinish


1 Answers

Could you please try following, completely based on showed samples only.

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
like image 52
RavinderSingh13 Avatar answered Dec 13 '25 15:12

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!