【问题标题】:Using symbols to correctly match columns in Awk?使用符号正确匹配 awk 中的列?
【发布时间】:2020-04-13 15:23:47
【问题描述】:

我有两个单独的文件,Input_File1 和 Input_File2,每个文件都包含不同数量的列,我根据多列中的数据合并 (with some help)。

至此,Input_File1 中添加了一列,根据 Input_File1 的第 1、2、3 列和 Input_File2 的第 1、2、3 列的数据匹配创建新文件(file3)。总的来说,这很好用。但是,在少数情况下,Input_File1 和 Input_File2 中的第 1、2、3 列中的数据相同,但在 file3 中的输出应该不同。这是基于 Input_File1 和 Input_File2 中的另一个特征,即“-”或“+”的存在。

Input_File1

VMNF01000007.1  6294425 6294650 .   .   +   Focub_B2_mimp_2
VMNF01000008.1  1441418 1441616 .   .   -   Focub_II5_mimp_3
VMNF01000008.1  1441418 1441616 .   .   -   Focub_B2_mimp_1
VMNF01000008.1  1441418 1441616 .   .   +   Focub_B2_mimp_2

Input_File2

VMNF01000007.1  6294425-6294650(+)  tacagtggggggcaataagtatgaataccctttggtgtactgacacacacctctt
VMNF01000008.1  1441418-1441616(-)  gggagtgtattgttttttctgccgctagcccattttaacatttagagtgtgcata
VMNF01000008.1  1441418-1441616(-)  gggagtgtattgttttttctgccgctagcccattttaacatttagagtgtgcata
VMNF01000008.1  1441418-1441616(+)  tacagtggggggcaataagtatgaataccctttgatgtactgacacacacctctt

可以看到,Input_File2的最后两行数据除了(-)和(+)是一样的,所以后面的顺序是不同的。

产生 file3 时,第 8 列中的序列与 Input_File2 中的序列没有区别。这是因为匹配列时只考虑数据VMNF01000008.1 1441418 1441616

当前文件3(注意序列和+或-最后两行):

VMNF01000007.1  6294425 6294650 .   .   -   Focub_B2_mimp_1 tacagtggggggcaataagtatgaataccctttggtgtactgacacacacctctt
VMNF01000008.1  1441418 1441616 .   .   -   Focub_II5_mimp_3 tacagtggggggcaataagtatgaataccctttgatgtactgacacacacctctt
VMNF01000008.1  1441418 1441616 .   .   -   Focub_B2_mimp_1 tacagtggggggcaataagtatgaataccctttgatgtactgacacacacctctt
VMNF01000008.1  1441418 1441616 .   .   +   Focub_B2_mimp_2 tacagtggggggcaataagtatgaataccctttgatgtactgacacacacctctt

file3 实际上应该看起来像(注意序列和 + 或 - 最后两行):

VMNF01000007.1  6294425 6294650 .   .   -   Focub_B2_mimp_1 tacagtggggggcaataagtatgaataccctttggtgtactgacacacacctctt
VMNF01000008.1  1441418 1441616 .   .   -   Focub_II5_mimp_3 gggagtgtattgttttttctgccgctagcccattttaacatttagagtgtgcata
VMNF01000008.1  1441418 1441616 .   .   -   Focub_B2_mimp_1 gggagtgtattgttttttctgccgctagcccattttaacatttagagtgtgcata
VMNF01000008.1  1441418 1441616 .   .   +   Focub_B2_mimp_2 tacagtggggggcaataagtatgaataccctttgatgtactgacacacacctctt

与 Input_File2 一样,当有“-”或“+”时,序列会有所不同。

所以它的操作方式与前面的代码大致相同,只是在 Input_File1 和 Input_File2 中添加了匹配“-”或“+”以确保后面的序列是正确的。如何使用“-”或“+”来确定应在第 8 列中添加到先前代码的序列?

这是我正在使用的代码 (from here):

awk '
FNR==NR{
  split($2,array,"[-(]")
  key=$1 OFS array[1] OFS array[2]
  mainarray[key]=$NF
  next
}
{ key = $1 OFS $2 OFS $3 }
(key in mainarray){
  print $0,mainarray[key]
}
'  Input_file2  Input_file1

有什么建议吗?谢谢

【问题讨论】:

    标签: awk multiple-columns


    【解决方案1】:

    请您尝试关注一下。

    awk '
    FNR==NR{
      split($2,array,"[-(]")
      key=$1 OFS array[1] OFS array[2]
      ++count1[key]
      mainarray[key OFS count1[key]]=$NF
      next
    }
    {
      key=$1 OFS $2 OFS $3
      ++count2[key]
    }
    ((key OFS count2[key]) in mainarray){
      print $0,mainarray[key OFS count2[key]]
    }
    '  Input_file2  Input_file1
    

    输出如下。

    VMNF01000007.1  6294425 6294650 .   .   +   Focub_B2_mimp_2 tacagtggggggcaataagtatgaataccctttggtgtactgacacacacctctt
    VMNF01000008.1  1441418 1441616 .   .   -   Focub_II5_mimp_3 gggagtgtattgttttttctgccgctagcccattttaacatttagagtgtgcata
    VMNF01000008.1  1441418 1441616 .   .   -   Focub_B2_mimp_1 gggagtgtattgttttttctgccgctagcccattttaacatttagagtgtgcata
    VMNF01000008.1  1441418 1441616 .   .   +   Focub_B2_mimp_2 tacagtggggggcaataagtatgaataccctttgatgtactgacacacacctctt
    

    说明:为上面添加详细说明。

    awk '                                          ##Starting awk program from here.
    FNR==NR{                                       ##Checking condition FNR==NR which will be TRUE when file2 is being read.
      split($2,array,"[-(]")                       ##Splitting 2nd field into array named array with separator -( in it.
      key=$1 OFS array[1] OFS array[2]             ##Creating variable key whose value is $1 array 1st element and array 2nd element.
      ++count1[key]                                ##Creating array count1 with index key and keep increasing its value with 1 here.
      mainarray[key OFS count1[key]]=$NF           ##Creating array mainarray with index key OFS count1[key] value and its value is last column value.
      next                                         ##next will skip all further statements from here.
    }
    {
      key=$1 OFS $2 OFS $3                         ##Creating variable key with value of first, second and third field values.
      ++count2[key]                                ##Creating array count2 with index key and keepincreasing value with 1 here.
    }
    ((key OFS count2[key]) in mainarray){          ##Checking condition if key OFS count2[key] is present in mainarray
      print $0,mainarray[key OFS count2[key]]      ##Printing current line and value of mainarray whose index is key OFS and value of count2  whose index is key.
    }
    '  Input_file2  Input_file1                    ##Mentioning Input_file names here.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 2018-07-28
      • 2022-06-13
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2013-06-01
      相关资源
      最近更新 更多