Lum – Column mismatch and substituting

awkcolumnsperlsedtext processing

input.txt (tab-delimted)

TTTTOTTT00000000008  RTTTT899      5.00E-28
TTTTOTTT00000000046  RTTTWRR       3.00E-31
TTTTOTTT00000000051  2.00E-11
TTTTOTTT00000000051  7.00E-12
TTTTOTTT00000000054  5.00E-22
TTTTOTTT00000000061  YTRYR         1.00E-11
TTTTOTTT00000000078  ETNRR8        6.00E-17
TTTTOTTT00000000174  TYTYT         1.00E-11
TTTTOTTT00000000203  UUUE          9.00E-20

I have file with column mixed up. In this file (input.txt). Column 3 has blank space, column should fill with column2 rows and column 2 rows should be substituted as none

output.txt (tab-delimted)

TTTTOTTT00000000008  RTTTT899      5.00E-28
TTTTOTTT00000000046  RTTTWRR       3.00E-31
TTTTOTTT00000000051  none          2.00E-11
TTTTOTTT00000000051  none          7.00E-12
TTTTOTTT00000000054  none          5.00E-22
TTTTOTTT00000000061  YTRYR         1.00E-11
TTTTOTTT00000000078  ETNRR8        6.00E-17
TTTTOTTT00000000174  TYTYT         1.00E-11
TTTTOTTT00000000203  UUUE          9.00E-20

Best Answer

awk -v 'OFS=\t' 'NF == 2 { print $1, "none", $2; next } 1' input.txt > output.txt

Adjust depending on the characteristics of your input file. I assume every line with only 2 fields should have a "none" inserted. Otherwise, all other lines are just passed through unchanged (the purpose of the 1 at the end).

Related Question