AWK If/ElseConditional Problem

Posted by neversaint on Stack Overflow See other posts from Stack Overflow or by neversaint
Published on 2010-04-12T04:56:54Z Indexed on 2010/04/12 5:03 UTC
Read the original article Hit count: 400

Filed under:
|
|
|

I have a data that looks like this:

foo foo      scaffold_7      1 4845 6422 4845
bar bar      scaffold_7      -1 14689 16310 16310

What I want to do is to process the above lines where I just want to print column 1,2,3, 7 and one more column after 7th. But with condition when printing column 7 onwards.

Below is my awk script:

awk '{ 
        if ($4=="+") { {end=$6-$5}{print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $7 "\t" end+$7} } 
        else 
            {end=$6-$5}{print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $7-end "\t" $7} 
    }'  

But why it doesn't achieve the desired result like this?

foo foo    scaffold_7      1       4845    6422
bar bar   scaffold_7      -1      14689   16310

Note that the arithmetic (e.g. $7-end or end+$7) is a must. So we can't just swap column from input file. Furthermore this AWK will be inside a bash script.

© Stack Overflow or respective owner

Related posts about awk

Related posts about unix