awk - Replace fields with values of other fields in the same line -


i have kind of input:

rs10000004 c t 4 rs10000004 0 75625312 c c c c t 0 c t  rs10000005 g 4 rs10000005 0 75625355 g 0 a g a  

i want substitute columns 8 end "a" if value in column identical 2nd field $2 or "b" if value identical third field $3. else, value printed (zero values expected in columns)

expected output

rs10000004 c t 4 rs10000004 0 75625312 a a b 0 b  rs10000005 g 4 rs10000005 0 75625355 0 b b b b b  

i tried following doesn't give me results empty lines. improving code better me show me new solution using other awk

cat input | awk '{ for(i=8; i<=nf; i++) { if($i == $2) $i="a"; else if($i == $3) $i="b"; else $i == 0; } print $i }' 

thanks in advance

code :

awk ' {     (i=8; i<=nf; i++) {        if ($i == $2) {            $i = "a";        }        else {            if ($i == $3) {                $i = "b";            }            else {                $i = 0;            }        }     }     print;         }' input 

or shorter :

awk ' {     (i=8; i<=nf; i++) {        if ($i == $2)            $i="a";        else            if ($i == $3)                $i="b";            else                $i = 0;     } } 1' input 

output :

rs10000004 c t 4 rs10000004 0 75625312 a a b 0 b  rs10000005 g 4 rs10000005 0 75625355 0 b b b b b  

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -