Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse a single line?

Status
Not open for further replies.

Meher1

Programmer
Jun 4, 2003
7
US
Hi I'm very new to unix and awk. here is what I want.

For ex: In input file FILE, * is the delimiter
If the file is :

ABC*12*avxcgju932*00**123*12345678iouyt*18*123nhtrkw *qw02334056518*1121211*U*00200*000000449*0*T*:!QR*XA*abcdefjpirewr*123rtyey145*19951212*1234*abc*Z*12345!XV*525*12345!CVB*99*123456789*


The script should look for QR and then if the next field is XA, then it should change the 6th field from there from a Z to K.
Th O/P should be like this:
ABC*12*avxcgju932*00**123*12345678iouyt*18*123nhtrkw *qw02334056518*1121211*U*00200*000000449*0*T*:!QR*XA*abcdefjpirewr*123rtyey145*19951212*1234*abc*K*12345!XV*525*12345!CVB*99*123456789*


TIA.



 
Try this

awk -f m.awk input-file

# ------ m.awk ------
BEGIN{OFS=FS="*"}
/QR/{
for (j=1;j<=NF;j++) {
if ($j ~ /QR/ && $(j+1)==&quot;XA&quot;) $(j+7)=&quot;K&quot;
}
}
{print}

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Perhaps this is slightly better

BEGIN{OFS=FS=&quot;*&quot;}
/QR/{
for (j=1;j<=NF-7;j++) {
if ($j ~ /QR/ && $(j+1)==&quot;XA&quot;) sub(/Z/,&quot;K&quot;,$(j+7))
}
}
{print}

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Thanks caKiwi! That was exactly what I wanted!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top