lundi 24 août 2015

AWK if then loop conditional on column values

I have 3 columns, I want to create a 4th column that is equal to the 2nd column only when the 3rd column is equal to 1 (otherwise, the value can be 0).

For example,
4 3 1
would become
4 3 1 3
whereas
4 3 2
would become
4 3 2 0

I tried it 3 ways, in all cases the 4th column is all zeroes:

'BEGIN {FS = "\t"}; {if ($3!=1) last=0; else last=$2} {print $1, $2, $3, last}'

'BEGIN {FS = "\t"}; {if ($3!=1) print $1, $2, $3, 0; else print $1, $2, $3, $2}'

'BEGIN {FS = "\t"}; {if ($3==1) print $1, $2, $3, $2; else print $1, $2, $3, 0}' 

Aucun commentaire:

Enregistrer un commentaire