The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 06-05-2008
subin_bala subin_bala is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 57
getting variable inside awk

Hi All,

I have awk script for replacing the nth ocurance of a string in an xml file...
My code is like this

FILETYPE=xml
TAGNAME=type
OCCURANCE=$1
TAGVALUE=valueur

echo OCCURANCE:$OCCURANCE
echo TAGNAME:$TAGNAME
echo TAGVALUE:$TAGVALUE

awk -v n=$OCCURANCE -v s="<$TAGNAME>$TAGVALUE</$TAGNAME>" '/< type(.+)type>/&&n==++c{sub("<type>(.+)</type>",s)}1' application.xml


this is working correctly .. but if ia m replacing the <type tag with $TAGNAME inside singlequotes, its not working...

awk -v n=$OCCURANCE -v s="<$TAGNAME>$TAGVALUE</$TAGNAME>" '/<$TAGNAME(.+)$TAGNAME>/&&n==++c{sub("<$TAGNAME>(.+)</$TAGNAME>",s)}1' application.xml

How do we use a variable in the awk command?

Thanks in advance