Nested if not working with /usr/xpg4/bin/awk

Hi,
I am trying to do if inside the If in /usr/xpg4/bin/awk. But I am getting below error :

/usr/xpg4/bin/awk -v a="${THREADIDARR
[*]}" 'BEGIN {FS="|"; n=split(a,b," "); for(i=1; i<=n; i++) c[b]=1;} length($3) == 0{ftag == 1{{print}}; length($3) != 0{$3 in c{ftag=1;print;} !$3 in c{ftag=0;}}' $INPUTFILE >> $OUTPUTFILE

Error :

/usr/xpg4/bin/awk: syntax error  Context is:
>>>      i<=n; i++) c[b]=1;} length($3) == 0{ftag == 1{      <<<

Can anyone suggest how I can implement the If - else and Nested If statement in /usr/xpg4/bin/awk.

Well, right off the top, you seem to have more { than } .. so that's probably confusing the heck out of things.

Definitely. Adding some whitespace to the situation:

/usr/xpg4/bin/awk -v a="${THREADIDARR[*]}" '
BEGIN {
  FS="|"
  n=split(a,b," ")
  for(i=1; i<=n; i++)
    c[b]=1
}

length($3) == 0 {
  ftag == 1

{{print}}

length($3) != 0 {
  $3 in c

{ftag=1;print;}

!$3 in c {ftag=0;}

}' $INPUTFILE >> $OUTPUTFILE

what's up with the extra {{print}} ? I think this could be simplified a lot more if we were given sample input and desired output.

1 Like

you need to wrap couple of statements in round brackets with if

/usr/xpg4/bin/awk -v a="${THREADIDARR[*]}" 'BEGIN {FS="|"; n=split(a,b," "); for(i=1; i<=n; i++) c[b]=1;} length($3) == 0{if(ftag == 1) {{print}}; length($3) != 0{if($3 in c) {ftag=1;print;} !($3 in c){ftag=0;}}' $INPUTFILE >> $OUTPUTFILE
1 Like

Thanks SriniShoo and neutronscott for great suggestion :b: :slight_smile:

I have changed following code and it is working fine.... Thanks a lot.... :smiley:

/usr/xpg4/bin/awk -v a="${THREADIDARR[*]}" 'BEGIN {FS="|"; n=split(a,b," "); for(i=1; i<=n; i++) c[b]=1;} length($3) == 0{if(ftag == 1) {{print}};} length($3) != 0{if($3 in c) {ftag=1;print;} if($3 in c == 0) {{ftag=0}};}' $INPUTFILE >> $OUTPUTFILE