Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
add comment

2 Answers

up vote 8 down vote accepted
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).

share|improve this answer
add comment
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.

share|improve this answer
    
or "none"OFS$2 to avoid hard-coding the TAB again. +1 –  Peter.O Aug 27 '12 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 '12 at 7:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.