This is the exact code I am running in my system with sh lookup.sh. I don’t see any details within nawk block printed or written to the file abc.txt. Only I am here 0 and I am here 1 are printed. Even the printf in nawk is not working. Please help.
processbody() {
nawk '
NR == FNR {
split($0, x, "@")
country_code[x[2]] = x[1]
next
system(" echo " I am here ">>/tmp/abc.txt")
}
{
CITIZEN_COUNTRY_NAME = "INDIA"
system(" echo " I am here 1">>/tmp/abc.txt")
if (CITIZEN_COUNTRY_NAME in country_code) {
value = country_code[CITIZEN_COUNTRY_NAME]
system(" echo " I am here 2">>/tmp/abc.txt")
} else {
value = "null"
system(" echo " I am here 3">>/tmp/abc.txt")
}
system(" echo " I am here 4">>/tmp/abc.txt")
print "found " value " for country name " CITIZEN_COUNTRY_NAME >> "/tmp/standalone.txt"
} ' /tmp/country_codes.config
echo "I am here 5" >> /tmp/abc.txt
}
# Main program starts here
echo "I am here 0" >> /tmp/abc.txt
processbody
And my country_codes.config file:
$ cat country_codes.config
IND@INDIA
IND@INDIB
USA@USA
CAN@CANADA
That’s some pretty interesting
awkcode. The problem is that your first condition, theNR == FNRone, is active for each record read from the first file – the country_codes.config file, but the processing action containsnextso after it reads a record and splits it and saves it, it goes and reads the next record – not executing the second block of theawkscript. At the end, it is done – nothing more to do, so it never prints anything.This works sanely:
It produces the output:
As Hai Vu notes, you can use
awk‘s intrinsic record splitting facilities to simplify life: