setting up a flag inside awk

Hi unix gurus....

i need to set a flag inside awk. i tried many methods for that but couldn't succeed :frowning:
like

echo "flag= " $flag
echo "flag= " ${flag}

and so...but not able to show the flag. help me out.
i'm using a if condition inside awk... help me out with correct syntax to set a flag and use it so that i will be able to return a value back to main script.......
thanks,

Not clear. Do you mean you are trying to pass a variable to awk?

awk 'BEGIN { print flag }' flag="1"

#or

awk -v glag="1" 'BEGIN { print flag }'

--ahamed

thanks for your reply....but solved just before that..... :slight_smile:

Appreciate if you could explain what was the issue and how you solved it. It will help others in the future.

--ahamed

hey Sorry, was busy, so replying late. Yeah I'd love to share that.
Just set a flag. Though directly were not able to set a flag. So whole command inside the flag. further took the flag value inside another var and cut the first field (as i need this one only) and used it further.may be it is the long way i used(there could be many ways) but it solved my problem :slight_smile:
Here its:

flag=""
flag=$( awk -F"|" -v var="$Property_Value" 'BEGIN{n=split(var,a,",")} NR > 1 { for(i=1;i<=n;i++) { if($a ~ /^ *$/){printf("1") }} }' $file)
 echo "flag = "$flag
 var1=$(echo $flag | cut -c1)

Regards,

One can also try this :wink:

 
eval $(awk -F"|" -v var="$Property_Value" 'BEGIN{n=split(var,a,",")} NR > 1 { for(i=1;i<=n;i++) { if($a ~ /^ *$/){printf("FLAG=1") }} }' $file)

If you do eval, then FLAG=1 will evaluated as shell command and FLAG will then become a variable.

This method using Eval and Awkis quite handy when you are trying to generate or evaluate variables that may get use by Shell.

So soimething like this:

eval $(awk '{print "X1="$1; print "X2="$2;}' someinputfile)

will give you two shell includable variable X1 and X2...

@knight_eon,

use the [ code ] tag, while posting the data or script

1 Like

how to mark this thread as solved?