4

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

2 Answers 2

8
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).

2
awk -v OFS="\t" 'NF==2{$2="none\t"$2}1' test_in.txt

Rather than having a separate print statement for the modified line, this patches fields where necessary and streams the corrected lines to be printed by the same statement.

2
  • or "none"OFS$2 to avoid hard-coding the TAB again. +1
    – Peter.O
    Aug 27, 2012 at 13:27
  • @Peter.O thanks for the +1. The OFS thing I'd think we can put down to a matter of style, but one thing I did copy from another awk solution is the use of 1 to get everything printed. I usually used an empty regexp match of // to do the same thing and the 1 does still seem strange, but I will remember it when maintaining others code.
    – Paddy3118
    Aug 29, 2012 at 7:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .