-2

I have the following file with the format shown below. how do I do a first pattern match on cell = XX, and edit a specific string after matching the cell=XX.

File.txt
{cell DFT_AXA
{naming A_1 A_2 A_3 A_4 A_5 B_1 B_2 B_3 C_1 C_2 C_3 D_1 D_2 D_3 D_4 D_5 D_6
E_1 E_2 F_1 F_2 F_3 G_1 G_2 G_3 
H_1 H_2 H_3 H_4
}

Output would be: if cell = DFX_AXA , replace G_2 with I_1.

Ali BAGHO
  • 358
  • 6
  • 17
Ginny
  • 7
  • 5
  • 3
    post the full expected result – RomanPerekhrest Aug 19 '17 at 10:25
  • Desired output : {cell DFT_AXA {naming A_1 A_2 A_3 A_4 A_5 B_1 B_2 B_3 C_1 C_2 C_3 D_1 D_2 D_3 D_4 D_5 D_6 E_1 E_2 F_1 F_2 F_3 G_1 **I_1** G_3 H_1 H_2 H_3 H_4 } – Ginny Aug 19 '17 at 10:57
  • @Ginny include the expected output in your question, not as a comment where it can't be formatted and people have to go looking for it. – Ed Morton Aug 19 '17 at 13:03

1 Answers1

0

Welcome to Stack overflow Ginny. Try following awk command too:

awk '/}/{a=""} /cell/ && $2=="DFT_AXA"{a=1} a && sub("G_2","I_1") 1'  Input_file

EDIT1: Adding non-one liner form solution with explanation too now as per OP's request.

awk '/}/{                #### Looking for } if any line is having this character, if yes then do following.
a=""                     #### making variable a value to NULL.
}
/cell/ && $2=="DFT_AXA"{ #### searching string cell and then checking if 2nd field is DFT_AXA, if yes then do following.
a=1                      #### making variable a as 1.
}
a && sub("G_2","I_1") 1  #### checking variable a value is NOT NULL and substitute G_2 with I_1 in a line, if both conditions are true line will be edited and 1 will print the line. awk works on condition/action method. If condition is TRUE then action should be done, if NO action is given like here so default action print will happen.
'  Input_file            #### Mentioning Input_file here.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    thanks Ravinder, btw, how does this works can you explain abit? awk '/}/{a=""} /cell/ – Ginny Aug 19 '17 at 10:56