Possible Duplicate:
UNIX shell script and escaping quotes for AWK script
I have a UNIX script that has nawk block inside it (This is just a part of the UNIX and NAWK script. It has many more logic and the below code should definitely be in nawk) This block that reads a lookup value for Country ISO code from a file that has country and country code values and I face an issue whenever there is a bracket in the country name () or a single apostrope '
These are just sample values and it can have many countries with single quote and parantheses
Sample values
CIV@COTE D'IVOIRE
COD@CONGO, Democratic Republic of (was Zaire)
HRV@CROATIA (local name: Hrvatska)
Can you pls help me overcome these 2 issues of if a string has a single quote or parantheses.
Is there anyway I can escape this single quote and apostrope dynamically ?
Code
processbody() {
nawk '{
// These are sample values not exhaustive
COUNTRY_NAME = "COTE D'IVOIRE" // this is read dynamically inside nawk
#COUNTRY_NAME = "CONGO, Democratic Republic of (was Zaire)" // this is read dynamically inside nawk
#COUNTRY_NAME = "CROATIA (local name: Hrvatska)" // this is read dynamically inside nawk
if (COUNTRY_NAME != " "){
file = "/tmp/country_codes.txt"
FS = "@"
while( getline < file ) {
if( $0 ~ COUNTRY_NAME ) {
COUNTRY_CODE = $1
}
}
close( file )
}
printf("%s\n",COUNTRY_CODE) > "/tmp/code.txt"
}' /tmp/file.txt
}
processbody