Stewart Russell
Stewart Russell

Reputation: 347

Awk to replace a column with another column if it meets criteria

In psudocode, I'm trying to replace column 4 with column 1, if column 4 equals NULL. Otherwise keep column 4 the same. I'm currently trying this, which isn't changing the line and I'm not sure why: Example data:

grep NULL matrix.txt

AAGGGCCCGGGGGG 0 0 3 NULL

grep NULL matrix.txt | awk -F/t '{ $34 = ($34 == "NULL" ? $1 : $34) } 1'

Should give me:

AAGGGCCCGGGGGG 0 0 3 AAGGGCCCGGGGGG

Thanks!

Upvotes: 0

Views: 351

Answers (1)

Rahul Verma
Rahul Verma

Reputation: 3089

modify your awk

replace $34 with $5 as you need to check 5th field not 34th

Also /t is incorrect. It should be \t for tab. BTW it will also work without -F "\t"

echo "AAGGGCCCGGGGGG 0 0 3 NULL"  | awk ' $5=="NULL"{$5=$1} 1'

Upvotes: 3

Related Questions