Four different conditions using awk if (unix)

2

Hello I need to do an awk if command using 4 different conditions together, example of my input only for a few lines (I have an input with thousands of lines)

chr17_30  1
chr1_72   0
chr1_46   2
chr1_47   -1
chr1_48   1

Output desirable

chr17_30  1 AB
chr1_72   0 AA
chr1_46   2 BB
chr1_47  -1 NN
chr1_48   1 AB

How do I do this? Only be able to do for a condition using awk if. It might be another command as well.

Thank you Clarissa

    
asked by anonymous 25.09.2015 / 14:58

1 answer

3
awk 'BEGIN{split("NN,AA,AB,BB",a,",")}  {print $0,a[$2+2]}' input.txt

Explanation:

  • At the beginning, split("NN,AA,AB,BB",a,",") defines an array "a" with a[1]=NN , a[2]=AA , a[3]=AB , etc. Notice that the indices are "2" below the $1
  • porting requires a[$2+2] to get the desired match.
28.09.2015 / 18:23