0

I have a file like this.

chr1    14969   14830   2   100(26) 100(31) 100(46) 100(13) 100(26) 100(30) 100(31) 100(36) 100(38)
chr1    15795   15039   2   100(3)  100(4)  100(4)  100(3)  100(6)  100(4)  100(2)  100(6)  100(6)
chr1    16606   15948   2   80(4)   *(0)    *(0)    100(1)  75(3)   100(3)  66.67(2)    100(3)  *(0)
chr1    16606   16311   2   20(1)   *(0)    *(0)    *(0)    25(1)   *(0)    33.33(1)    *(0)    100(1)
chr1    16853   16766   2   *(0)    *(0)    *(0)    *(0)    *(0)    *(0)    100(1)  *(0)    100(2)
chr1    16857   16766   2   100(2)  100(5)  100(5)  100(1)  100(2)  100(2)  100(2)  100(4)  100(2)
chr1    17232   17056   2   100(2)  100(4)  100(8)  100(2)  100(2)  100(7)  100(6)  100(5)  100(3)
chr1    17525   17369   2   *(0)    *(0)    100(2)  *(0)    *(0)    *(0)    *(0)    *(0)    *(0)
chr1    17605   17056   1   50(9)   38.89(7)    57.89(11)   30(3)   *(0)    *(0)    40.74(11)   65.52(19)   57.14(16)
chr1    17605   17369   1   50(9)   61.11(11)   42.11(8)    70(7)   100(3)  100(8)  59.26(16)   34.48(10)   42.86(12)

I want to swap column 3 with column 2 and also if column 4 is a 2 replace it with - else with +. The output should look like this.

I would like to do this in awk. I tried this to swap the columns. Also I want header line in my output as it is

awk -F '\t' -v OFS="\t" 'NR > 1{a=$2; $2=$3; $3=a;} 1' file

How can I change the column 4 to + and - depending upon whether it is 1 or 2 and then print whatever number of columns I have after 4 th column?

output

chr1    14969   14830   -   100(26) 100(31) 100(46) 100(13) 100(26) 100(30) 100(31) 100(36) 100(38)
chr1    15795   15039   -   100(3)  100(4)  100(4)  100(3)  100(6)  100(4)  100(2)  100(6)  100(6)
chr1    16606   15948   -   80(4)   *(0)    *(0)    100(1)  75(3)   100(3)  66.67(2)    100(3)  *(0)
chr1    16606   16311   -   20(1)   *(0)    *(0)    *(0)    25(1)   *(0)    33.33(1)    *(0)    100(1)
chr1    16853   16766   -   *(0)    *(0)    *(0)    *(0)    *(0)    *(0)    100(1)  *(0)    100(2)
chr1    16857   16766   -   100(2)  100(5)  100(5)  100(1)  100(2)  100(2)  100(2)  100(4)  100(2)
chr1    17232   17056   -   100(2)  100(4)  100(8)  100(2)  100(2)  100(7)  100(6)  100(5)  100(3)
chr1    17525   17369   -   *(0)    *(0)    100(2)  *(0)    *(0)    *(0)    *(0)    *(0)    *(0)
chr1    17605   17056   +   50(9)   38.89(7)    57.89(11)   30(3)   *(0)    *(0)    40.74(11)   65.52(19)   57.14(16)
chr1    17605   17369   +   50(9)   61.11(11)   42.11(8)    70(7)   100(3)  100(8)  59.26(16)   34.48(10)   42.86(12)

Thanks

  • Hi, I have up voted most of the answers. Wasn't aware how to accept it. Doing it now. But if you look , I have up voted most of the answers. – user3138373 Dec 2 '16 at 17:44
  • Sorry didn't know about accepting the answer, thought up voting an answer meant acceptance :( – user3138373 Dec 2 '16 at 17:50
2

With the following awk script named for example script.awk:

{
    temp=$2
    $2=$3
    $3=temp
    if ( $4 == 2 ) {
        $4="-"
    } else {
        $4="+"
    }
    print
    temp=''
}

The following appears to do what you want:

$ awk -F '\t' -v OFS='\t' -f script.awk input
  • Can you tell the use of temp='' at the end. Also after seeing your code this works for me on command line awk -F '\t' -v OFS="\t" 'NR==1{next};{a=$2; $2=$3; $3=a; if( $4 == 2 ) { $4="-"} else {$4="+"} } 1' – user3138373 Dec 2 '16 at 18:03
  • 2
    clearing out temp is just a sanity thing to make sure that it's not left set to accidentally clutter later things. It would certainly work as a one-(very long)-liner, but I thought writing it out would make it simpler to understand how it works. – DopeGhoti Dec 2 '16 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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