Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux AWK If/ElseConditional问题_Linux_Bash_Unix_Awk - Fatal编程技术网

Linux AWK If/ElseConditional问题

Linux AWK If/ElseConditional问题,linux,bash,unix,awk,Linux,Bash,Unix,Awk,我的数据如下所示: foo foo scaffold_7 1 4845 6422 4845 bar bar scaffold_7 -1 14689 16310 16310 我想做的是处理上面的行 我只想打印第1、2、3、7列,在第7列之后再打印一列。 但在打印第7列以后的内容时有条件 以下是我的awk脚本: awk '{ if ($4=="+") { {end=$6-$5}{print $1 "\t" $2 "\t" $3 "\t" $

我的数据如下所示:

foo foo      scaffold_7      1 4845 6422 4845
bar bar      scaffold_7      -1 14689 16310 16310
我想做的是处理上面的行 我只想打印第1、2、3、7列,在第7列之后再打印一列。 但在打印第7列以后的内容时有条件

以下是我的awk脚本:

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} 
    }'  
但为什么它不能达到这样的预期效果呢

foo foo    scaffold_7      1       4845    6422
bar bar   scaffold_7      -1      14689   16310
请注意,算术运算(例如
$7-end
end+$7
)是必须的。所以我们不能只交换列 从输入文件。此外,此AWK将位于bash脚本中。

请尝试:

awk 'BEGIN {OFS = "\t"} { 
    end = $6 - $5
    if ($4 >= 0) {
        print $1, $2, $3, $4, $7, end + $7
    } else {
        print $1, $2, $3, $4, $7 - end, $7
    }
}'
我从未在大括号中使用大括号,因此更喜欢使用分号分隔符。我非常确定else子句在您的示例中只绑定到
{end=$6-$5}
,而不是绑定到两个带大括号的条目。在这种情况下,将对所有行执行
{print$1”\t“$2”\t“$3”\t“$4”\t“$7-end”\t“$7}

awk'{end=$6-$5}
$4>0{$0=$1“\t“$2”\t“$3”\t“$4”\t“$7”\t”end+$7}

$4更简洁、更短、更易于更改:
awk-vOFS='\t'{…打印$1、$2…
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