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.

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.

I am new to awk and still learning.

Thanks

share|improve this question

1 Answer 1

up vote 5 down vote accepted

This may work

awk '$5 == "+" {$2-=5000;$3+=2000}; $5 == "-"{$3+=5000;$2-=2000};{print}'  file
share|improve this answer
    
This works!! Also I would like to print the header line #track –  user3138373 May 1 at 20:36
    
@user3138373, this script already does print the header line –  1_CR May 1 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 –  user3138373 May 1 at 20:45
1  
@user3138373, semicolons are used to separate in both those instances –  1_CR May 1 at 20:49

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.