tags:

views:

41

answers:

2

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.

+2  A: 

Try:

awk '{ 
    if ($4>=0) {
        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
    }
}'

I've never used braces within braces and prefer the semi-colon separators for this purpose. I'm pretty certain the else clause is bound only to {end=$6-$5} in your example, not to both the braced entries. In that case, {print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $7-end "\t" $7} will be executed for all lines.

paxdiablo
Neater, shorter, easier to change: `awk -vOFS='\t' '{ ... print $1, $2 ... `
Dennis Williamson
+2  A: 
awk '{end=$6-$5}
$4>0{ $0=$1 "\t" $2 "\t" $3 "\t" $4 "\t" $7 "\t" end+$7 }
$4<=0{$0=$1 "\t" $2 "\t" $3 "\t" $4 "\t" $7-end "\t" $7 }1'  file
ghostdog74