Handling 3 Files using awk

awkgawktext processing

Consider following files:

file1:

boo,8,1024
foo,7,2048

file2:

foo,0,24,154
noo,0,10,561

file3:

24,154,7,1024,0

What I need is to go to File1 and check if $2==7; if true, take $1, $2 and $3 from File1; now I have to compare if $1 from File1 equal to $1 from File2; if true, I have to take $3 and $4 from File2 which not exist in File1, then I have to go to File3 and check if $1 from File3 is equal to $3 from File2, and $2 from File3 is equal to $4 from File2; if yes, then I have to check if $2 from File1 is equal to $3 from File3, then if this condition is true, I have to compare $3 from File1 with $4 from File3, if $3 from File1 is more than $4 from File3.

I tried the following script:

cat [file1] [file2] [file3] | 
awk -F, 
'{if(NF==3)
    {if($2==7){a[$1]=$1; b[$1]=$2; c[$1]=$3}
    }else
        {if(NF==4){if(a[$1]==$1){d[$3]=$3; e[$4]=$4}
                  }else
                        {if(NF==5){if(d[$1]==$1 && e[$2]==$2){print a[$1], b[$1], c[$1], d[$1]}}
                        }
                  }

  }'

The desired output is:

foo,7,2048,24,154,1024

Best Answer

That worked for me:

awk -F, 'FNR==1{++f} \
  f==1 && $2==7 {a1[$1]++; a2[$2]=$3; o=$0} \
  f==2 && a1[$1] {o=o","$3","$4; a3[$3]=$4} \
  f==3 && a3[$1] && $2==a3[$1] && a2[$3] && $4<a2[$3] {print o}' \
file1 file2 file3

Explanation:

  • The first line (FNR==1{++f}) increments the file index to later determine in which file we are 1-3.
  • file1: if $2 equals 7
    • fill an array a1 with $1 as index and a2 with $2 as index and $3 as value
    • write down the o variable (output) with the first 3 fields
  • file2: if $1 of file2 equals $1 of file1 (prevously written in a1)
    • append $3 and $4 to the output variable o.
    • fill an array a3 with $3 as index and $4 as value.
  • file3: if:
    • $1 equals file2s $3 (index of a3)
    • $2 equals file2s $4 (value of a3)
    • $3 equals file1s $2 (index of a2)
    • $4 is lower than file1s $3 (value of a2)
  • then:
    • print the value of o.
Related Question