2

I have an input file in the following format.

#track
chr11   61731756    61735132    FTH1    -
chr12   6643584 6647537 GAPDH   +
chr11   18415935    18429765    LDHA    +
chr12   21788274    21810728    LDHB    -
chr22   24236564    24237409    MIF +
chr4    6641817 6644470 MRFAP1  +
chr15   72491369    72523727    PKM -
chr10   73576054    73611082    PSAP    -
chr2    85132762    85133799    TMSB10  +
chr13   45911303    45915297    TPT1    -

I want to get an output file in which if the 5th column is + then subtract 5000 from column 2 and add 2000 to column 3 so as to output following

chr11   18410935    18431765    LDHA    +
chr2    85127762    85135799    TMSB10  +

If the 5th column is "-", then add 5000 to column 3 and subtract 2000 from column 2 so as to get following output

chr11   61729756    61740132    FTH1    -
chr12   21786274    21815728    LDHB    -

So I want the output to be in the same order as input. Also I want to output the first line #track as it is.

I want to do this in awk. How can I perform this.

1 Answer 1

5

This may work

awk '$5 == "+" {$2-=5000;$3+=2000}; $5 == "-"{$3+=5000;$2-=2000};{print}'  file
4
  • This works!! Also I would like to print the header line #track May 1, 2014 at 20:36
  • @user3138373, this script already does print the header line
    – iruvar
    May 1, 2014 at 20:38
  • got it, it does print the header line. Also I want to ask you awk is basically pattern action statements, but within action we can have many statements separated by ;?? and of course different pattern action statements are separated by colon May 1, 2014 at 20:45
  • 1
    @user3138373, semicolons are used to separate in both those instances
    – iruvar
    May 1, 2014 at 20:49

You must log in to answer this question.

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