print to more than one outfile AWK

I'd like to do an if / else if condition and print to different files.

Something like:

awk '{ 
      if ($1 == "yes")  print $2, $4 < infile > outfile1 ;
else if ($1 == "No")   print $2, $4 < infile > outfile2  

}' 

Obviously I don't know the syntax.

Thanks so much.

awk '$1=="yes"{print $2, $4 > outfile1} $1=="no"{print $2, $4 > outfile2}' infile

-or-

awk '{if ($1=="yes"){print $2, $4 > "outfile1"} 
      if($1=="no"){print $2, $4 > "outfile2"}
     }' infile

-or-

awk '{p=($1=="yes")?1:2; print $2, $4 > "outfile"p}' infile
awk '
  $1 == "yes" { print $2, $4 > "outfile1" }
  $1 == "no" { print $2, $4 > "outfile2" }
' infile

Thanks so much. That worked.

:slight_smile:

gawk '$1~/^(yes|no)$/{ print $2,$4 > "outfile."$1}' inputfile 

No-where in the OP's question did I see where the output files should be called output.yes or output.no...

I don't have gawk, and your code doesn't work with awk.