Multi-conditional IF - awk

I have a 6 column array with 2 million rows that looks like this:

1 1089699 rs6686003 G A g     
1 1090557 rs7553429 A C c     
1 1094738 rs4970362 A G a     
1 1099342 rs9660710 A C c     
1 1106473 rs4970420 G A a     
1 1108637 rs4970421 G A g     
1 1119858 rs1320565 C T c     
1 1120431 rs1320571 G A A  

I need to change all lower case letters in the 6th column to uppercase. I wrote the following script, but all it is doing is changing every value in the 6th column to "A"

awk '{ 
if ($6="a") $6="A"; 
else if ($6="c") $6="C"; 
else if ($6="g") $6="G"; 
else if ($6="t") $6="T"; 
print $0;
}'

This is what I'm getting:

1 1089699 rs6686003 G A A
1 1090557 rs7553429 A C A
1 1094738 rs4970362 A G A
1 1099342 rs9660710 A C A
1 1106473 rs4970420 G A A
1 1108637 rs4970421 G A A
1 1119858 rs1320565 C T A
1 1120431 rs1320571 G A A

I have Ubuntu 16.04 LTS. Any help would be greatly appreciated.

Note that = is an assignment operator. It assigns the value of right side expression to left side.

But == is an equal to operator. It compares the value of both sides. Hence you have to use == in your if condition.

1 Like

Thanks Yoda, works like a charm :slight_smile:

awk '
{
if (str ~ ":" $6 ":") $6=toupper($6);
print $0;
}' str=":a:c:g:t:" datafile

or

awk '$6=toupper($6)' datafile
1 Like

Based on your sample, where there is NO other field containing "a","c","g", or "t", this could work:

sed 'y/acgt/ACGT/' file
1 Like

Nice Rdtrx1! A little more limited than my script but fine for this purpose