【问题标题】:Merging columns in sliding window steps在滑动窗口步骤中合并列
【发布时间】:2020-10-29 20:13:52
【问题描述】:

我有一个制表符分隔文件

NC_044998.1     3778    0       CC      0       CC      0       CC      0       CC      1       CT      0       CC      0       CC      0       CC      0       CC      1       CT      0      CC       var     heterozygous    varvar  9       0.818182        2       0.181818        refref  refref  refref  refref  refdev  refref  refref  refref  refref  refdev  refref  homo    homo   homo     homo    het     homo    homo    homo    homo    het     homo    9       0       2       0       9       2
NC_044998.1     3787    0       CC      0       CC      1       CG      0       CC      0       CC      0       CC      0       CC      0       CC      0       CC      0       CC      0      CC       var     heterozygous    varvar  10      0.909091        1       0.0909091       refref  refref  refdev  refref  refref  refref  refref  refref  refref  refref  refref  homo    homo   het      homo    homo    homo    homo    homo    homo    homo    homo    10      0       1       0       10      1

$32-$43 的列有某种信息 (ref/dev),而$43-$54 的列有另一个 (homo/het) 我想合并它们,以便 col $32 与 col $32 + 11 = $43 合并并附加到文件的末尾,然后移动 i + 1,以便合并 $33$44 并附加,然后是$34$45,依此类推。 输出看起来像

NC_044998.1     3778    0       CC      0       CC      0       CC      0       CC      1       CT      0       CC      0       CC      0       CC      0       CC      1       CT      0      CC       var     heterozygous    varvar  9       0.818182        2       0.181818        refref  refref  refref  refref  refdev  refref  refref  refref  refref  refdev  refref  homo    homo   homo     homo    het     homo    homo    homo    homo    het     homo    9       0       2       0       9       2   refrefhomo  refrefhomo  refrefhomo  refrefhomo  refdevhet  refrefhomo  refrefhomo  refrefhomo  refrefhomo  refdevhet refrefhomo
NC_044998.1     3787    0       CC      0       CC      1       CG      0       CC      0       CC      0       CC      0       CC      0       CC      0       CC      0       CC      0      CC       var     heterozygous    varvar  10      0.909091        1       0.0909091       refref  refref  refdev  refref  refref  refref  refref  refref  refref  refref  refref  homo    homo   het      homo    homo    homo    homo    homo    homo    homo    homo    10      0       1       0       10      1   refrefhomo  refrefhomo  refdevhet  refrefhomo  refrefhomo  refrefhomo  refrefhomo  refrefhomo  refrefhomo  refrefhomo  refrefhomo

我可以一个一个的

 cut -f32,43 file | sed 's/ //g' | paste file - > file.tmp

但它会生成 11 个 tmp 文件

【问题讨论】:

    标签: bash awk gsub


    【解决方案1】:

    你可以试试这个awk:

    awk 'BEGIN {
       FS=OFS="\t"
    }
    {
       for (i=32; i<43; ++i)
          $0 = $0 OFS $i $(i+11)
    } 1' file
    
    NC_044998.1 3778    0   CC  0   CC  0   CC  0   CC  1   CT  0   CC  0   CC  0   CC  0   CC  1   CT  0   CC  var heterozygous    varvar  9   0.818182    2   0.181818    refref  refref  refref  refref  refdev  refref  refref  refref  refref  refdev  refref  homo    homo    homo    homo    het homo    homo    homo    homo    het homo    9   0   2   0   9   2   refrefhomo  refrefhomo  refrefhomo  refrefhomo  refdevhet   refrefhomo  refrefhomo  refrefhomo  refrefhomo  refdevhet   refrefhomo
    NC_044998.1 3787    0   CC  0   CC  1   CG  0   CC  0   CC  0   CC  0   CC  0   CC  0   CC  0   CC  0   CC  var heterozygous    varvar  10  0.909091    1   0.0909091   refref  refref  refdev  refref  refref  refref  refref  refref  refref  refref  refref  homo    homo    het homo    homo    homo    homo    homo    homo    homo    homo    10  0   1   0   10  1   refrefhomo  refrefhomo  refdevhet   refrefhomo  refrefhomo  refrefhomo  refrefhomo  refrefhomo  refrefhomo  refrefhomo  refrefhomo
    

    【讨论】:

    • 谢谢!这应该让我的文本处理周结束!
    • 你的问题很吸引人
    【解决方案2】:

    像@anubhava sir's 这样的答案在逻辑上很明智。带有变量和一个小的附加逻辑,将数字添加到行尾,最大字段数与起始字段数不同。

    awk -v startFieldnum="32" -v tillFieldnum="43" '
    BEGIN{
       FS=OFS="\t"
       diff=(tillFieldnum-startFieldnum)
    }
    {
       for (i=startFieldnum; i<tillFieldnum ; ++i)
          $0 = $0 OFS $i $(i+diff)
    } 1' Input_file
    

    【讨论】:

      猜你喜欢
      • 2020-09-19
      • 2015-11-08
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 2023-03-16
      • 2021-06-14
      • 1970-01-01
      相关资源
      最近更新 更多