awk将字段长度存储在变量中,然后在打印中使用

时间:2018-06-20 12:36:37

标签: awk

在下面的awk中,如果条件满足(在两行中),我试图将$5的长度存储在变量il中,然后添加该变量到$3语句中的print。这两个sub语句将从$5$6中删除匹配项。脚本按原样执行并产生当前输出。但是,il似乎没有被填充并添加到print中。看起来很近,但是我不确定为什么不存储该变量?谢谢:)

awk

awk 'BEGIN{FS=OFS="\t"}  # define fs and output
   FNR==NR{ # process each field in each line of file
     if(length($5) < length($6)) {  # condition
      il=$(length($5))
       echo $il
        sub($5,"",$6) && sub($6,"",$5)       # removing matching
         print $1,$2,$3+$il,$3+$il,"-",$6  # print desired output
           next
}
}' in

tab-delimited

id1 1   116268178       GAAA    GAAAA
id2 2   228197304       A   AATCC

当前输出 tab-delimited

id1 1   116268178   116268178   -   A
id2 2   228197304   228197304   -   ATCC

所需的输出 tab-delimited

since `$5` is 4 in line 1 that is added to `$3`
since `$5` is 1 in line 2 that is added to `$3`

id1 1   116268181   116268181   -   A
id2 2   228197305   228197305   -   ATCC

1 个答案:

答案 0 :(得分:1)

遵循awk可能会对您有所帮助。

awk '{$3+=length($4);$3=$3 OFS $3;sub($4,"",$5);$4="-"} 1' Input_file

如果您的Input_file用TAB分隔,并且您也需要以TAB分隔形式输出,请添加BEGIN{FS=OFS="\t"}