geting a value out of awk script

the value of flag is reset inside the awk statments and is not getting echoed out of it. how can this be done??

#!/bin/ksh
flag=0
awk -F '[;]' '$2 == "ABCD" && $6 == "MNOP"{flag=1}'  file.xml
echo $flag

I am always getting the value of flag as "0" , even after the $2 is "ABCD" and $6 is "MNOP"

please correct where i am wrong??

One way:

flag=$(awk -F ';' '$2 == "ABCD" && $6 == "MNOP" {print 1}'  file.xml)

Inside AWK, setting flag would set an AWK variable, not a shell variable.

1 Like

In general,
unix variables are not available inside awk command
awk command variables are not available to unix

however,
awk -v myvar=$script_var ...
would set the variable myvar within awk programming to the previously set $script_var value
awk would need to output a variable value in order for it to be available outside of the awk command.
see: Passing values out awk.