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_开发者_StackOverflowcode) {
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 awk
code. The problem is that your first condition, the NR == FNR
one, is active for each record read from the first file - the country_codes.config file, but the processing action contains next
so after it reads a record and splits it and saves it, it goes and reads the next record - not executing the second block of the awk
script. At the end, it is done - nothing more to do, so it never prints anything.
This works sanely:
processbody()
{
awk '
{
split($0, x, "@")
country_code[x[2]] = x[1]
#next
}
END {
CITIZEN_COUNTRY_NAME = "INDIA"
if (CITIZEN_COUNTRY_NAME in country_code) {
value = country_code[CITIZEN_COUNTRY_NAME]
} else {
value = "null"
}
print "found " value " for country name " CITIZEN_COUNTRY_NAME
} ' /tmp/country_codes.config
}
# Main program starts here
processbody
It produces the output:
found IND for country name INDIA
As Hai Vu notes, you can use awk
's intrinsic record splitting facilities to simplify life:
processbody()
{
awk -F@ '
{ country_code[$2] = $1 }
END {
CITIZEN_COUNTRY_NAME = "INDIA"
if (CITIZEN_COUNTRY_NAME in country_code) {
value = country_code[CITIZEN_COUNTRY_NAME]
} else {
value = "null"
}
print "found " value " for country name " CITIZEN_COUNTRY_NAME
} ' /tmp/country_codes.config
}
# Main program starts here
processbody
I don't know what you want to accomplish, but let me guess: if country is INDIA, then print the following output:
found IND for country name INDIA
If that is the case, the following code will accomplish that goal:
awk -F@ '/INDIA/ {print "found " $1 " for country name " $2 }' /tmp/country_codes.config
The -F@ flag tells awk (or nawk) to use @ as the field separator.
@user549432 I think that you want one awk script that first reads in the country codes file and builds the associative array, and then reads in the input files (not @ delimited) and does a substitution?
if so, let's assume that /tmp/country_codes.config has:
IND@INDIA
IND@INDIB
USA@USA
CAN@CANADA
and /tmp/input_file (not @ delimited) has:
I am from INDIA
I am from INDIB
I am from CANADA
Then, we can have a nawk script like this:
nawk '
BEGIN {
while (getline < "/tmp/country_codes.config")
{
split($0,x,"@")
country_code[x[2]] = x[1]
}
}
{ print $1,$2,$3,country_code[$4]}
' /tmp/input_file
The output will be:
I am from IND
I am from IND
I am from CAN
精彩评论