【问题标题】:Merging two txt files based on a common column with different row numbers基于具有不同行号的公共列合并两个txt文件
【发布时间】:2021-05-19 00:29:45
【问题描述】:

我想合并两个以空格分隔的文件,而不先根据“表型”列对它们进行排序。文件 1 多次包含相同的表型,而文件 2 每个表型只有一次。我需要将文件 1 中的“表型”与文件 2 中的“类别”相匹配。

文件 1:

chr pos pval_EAS phenotype FDR
1 1902906 0.234 biomarkers-30600-both_sexes-irnt.tsv.gz 1
2 1475898 0.221 biomarkers-30600-both_sexes-irnt.tsv.gz 1
2 568899 0.433 continuous-4566-both_sexes-irnt.tsv.gz 1
2 2435478 0.113 continuous-4566-both_sexes-irnt.tsv.gz 1
4 1223446 0.112 phecode-554-both_sexes-irnt.tsv.gz 0.345
4 3456573 0.0003 phecode-554-both_sexes-irnt.tsv.gz 0.989

文件 2:

phenotype Category
biomarkers-30600-both_sexes-irnt.tsv.bgz Metabolic
continuous-4566-both_sexes-irnt.tsv.gz Neoplasms
phecode-554-both_sexes-irnt.tsv.gz Immunological

我尝试了以下方法,但没有得到所需的输出:

awk -F' ' 'FNR==NR{a[$1]=$4; next} {print $0 a[$6]}' file2 file1 > file3

【问题讨论】:

    标签: linux awk merge


    【解决方案1】:

    使用您展示的示例,请尝试以下操作。

    awk 'FNR==NR{arr[$1]=$2;next} ($4 in arr){print $0,arr[$4]}' file2 file1
    

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

    awk '                ##Starting awk program from here.
    FNR==NR{             ##Checking condition which will be TRUE when file2 is being read.
      arr[$1]=$2         ##Creating array arr with index of $1 and value is $2.
      next               ##next will skip all further statements from here.
    }
    ($4 in arr){         ##Checking condition if 4th field is in arr then do following.
      print $0,arr[$4]   ##Printing current line along with value of arr with 4th field as index number.
    }
    ' file2 file1        ##Mentioning Input_file names here.
    

    奖励解决方案:如果您想打印那些不匹配值的行并希望使用N/A 进行打印,请执行以下操作。

    awk 'FNR==NR{arr[$1]=$2;next} {print $0,(($4 in arr)?arr[$4]:"N/A")}' file2 file1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2014-07-12
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2020-01-07
      相关资源
      最近更新 更多