2017-08-19 3 views
-2

J'ai le fichier suivant au format indiqué ci-dessous. Comment puis-je faire une première correspondance de modèle sur cell = XX, et modifier une chaîne spécifique après avoir correspondu à cell=XX.Modification du deuxième motif de correspondance après le premier motif de correspondance avec sed/awk

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 
} 

sortie serait: si cell = DFX_AXA, remplacez G_2 avec I_1.

+3

Postez le résultat attendu complet – RomanPerekhrest

+0

sortie désiré: {{cellule DFT_AXA nommant 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

+0

S'il vous plaît jeter un oeil à: [Que dois-je faire quand quelqu'un répond à ma question?] (Http://stackoverflow.com/help/someone-answers) – Cyrus

Répondre

0

Bienvenue dans Stack overflow Ginny. Essayez la commande awk suivante aussi:

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

EDIT1: Ajout d'une solution de forme non une doublure avec des explications aussi maintenant par la demande de OP.

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. 
+1

merci Ravinder, btw, comment cela fonctionne pouvez-vous expliquer abit? awk '/}/{a = ""}/cell/ – Ginny

+0

@Ginny: Heureux que cela a fonctionné, j'ai aussi ajouté des explications maintenant. Voir aussi https://stackoverflow.com/help/someone-answers – RavinderSingh13