Four different conditions using awk if (Unix)

Asked

Viewed 244 times

2

Hello I need to do an awk if command using 4 different conditions together, example of my input just 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

Desirable output

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 able to do one condition using awk if. It can be another command too.

thank you Clarissa

  • how do you determine the added column? where the (AB,AA,...) come from? What did you try?

  • hi @jjoao this encoding is pre-established for my genetic data. Code 1 means my pet is AB, 2 is BB, -1 is given lost (NN) and 0 is AA. What I did was use the command: cat input.txt | awk 'BEGIN{FS=" t"}; BEGIN{OFS=" t"}; {if ($2==1) {print $0,"AB"} Else{print $0, " t"}}' | awk 'BEGIN{FS=" t"}; BEGIN{OFS=" t"}; {if ($2=0) {print $0,"AA"} Else{print $0, " t"}}' | head But in this case the encoding is in separate columns and I want everything in the same column.

1 answer

3

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

Explanation:

  • At first, split("NN,AA,AB,BB",a,",") defines an array "a" with a[1]=NN, a[2]=AA, a[3]=AB, etc. Note that the indices are "2" below the field $1
  • therefore it is necessary a[$2+2] to obtain the desired correspondence.
  • I’m sorry but I don’t understand the command. I use this to join the columns I created with AA, AB, NN and BB? What would $2+2 be?

  • Taking for example the case of line 4, $2+2 fetch the value contained in the second column of the input (-1) and adds to it 2 (getting 1); a[1] is NN so write chr1_47 -1 NN.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.