我有两个用于比较两个文件的脚本。
第一个脚本比较$ 3列:
> awk -v OFS="\t" 'NR==FNR{a[$3]=$4;next}{$2=$2 "\t"(a[$2]?a[$2]:"-")}1' file1 file2
第二个脚本比较$ 2列:
> awk -v OFS="\t" 'NR==FNR{a[$2]=$4;next}{$2=$2 "\t"(a[$2]?a[$2]:"-")}1' file1 file2
您看到差异为NR==FNR{a[$3]=$4;next}
和
NR==FNR{a[$2]=$4;next}
和我想写入一个脚本,所以类似于
NR==FNR{a[$2 || $3]=$4}
INPUT: 档案1
chr1 11796320 11796321 MTHFR
chr1 169549810 169549811 F5
chr1 173917077 173917078 SERPINC1
chr2 48962781 48962782 FSHR
chr4 121696961 121696962 ANXA5
chr4 121697010 121697011 ANXA5
chr4 121697036 121697037 ANXA5
chr4 121697055 121697056 ANXA5
chr11 46739504 46739505 F2
chr13 20189510 20189511 GJB2
chr13 20189546 20189547 GJB2
文件2
chr1 11796321 G 0 WILD ADP=1026
chr1 169549811 C 0 WILD ADP=940
chr1 173917078 C 0 WILD ADP=501
chr2 48962782 C T HET ADP=1665
chr4 121696962 C T HET ADP=212
chr4 121697011 A 0 WILD ADP=184
chr4 121697037 T 0 WILD ADP=111
chr4 121697037 tccc 0 INDEL AINDEL
chr4 121697056 C 0 WILD ADP=112
chr11 46739505 G 0 WILD ADP=202
chr13 20189511 C 0 WILD ADP=326
chr13 20189546 AC A INDEL ADP=164
chr13 20189547 C 0 WILD ADP=3
输出:
chr1 11796321 MTHFR G 0 WILD ADP=1026
chr1 169549811 F5 C 0 WILD ADP=940
chr1 173917078 SERPINC1 C 0 WILD ADP=501
chr2 48962782 FSHR C T HET ADP=1665
chr4 121696962 ANXA5 C T HET ADP=212
chr4 121697011 ANXA5 A 0 WILD ADP=184
chr4 121697037 ANXA5 T 0 WILD ADP=111
chr4 121697037 ANXA5 tccc 0 INDEL AINDEL
chr4 121697056 ANXA5 C 0 WILD ADP=112
chr11 46739505 F2 G 0 WILD ADP=202
chr13 20189511 GJB2 C 0 WILD ADP=326
chr13 20189546 GJB2 AC A INDEL ADP=164
chr13 20189547 GJB2 C 0 WILD ADP=3
答案 0 :(得分:1)
另一个awk
(已编辑为包含$4
):
awk 'FNR==NR{A[$3]=$1 FS $3 FS $4;next} ($2 in A){print A[$2],$3,$4,$5,$6}' file1 file2
chr1 11796321 MTHFR G 0 WILD ADP=1026
chr1 169549811 F5 C 0 WILD ADP=940
chr1 173917078 SERPINC1 C 0 WILD ADP=501
chr2 48962782 FSHR C T HET ADP=1665
chr4 121696962 ANXA5 C T HET ADP=212
chr4 121697011 ANXA5 A 0 WILD ADP=184
chr4 121697037 ANXA5 T 0 WILD ADP=111
chr4 121697037 ANXA5 tccc 0 INDEL AINDEL
chr4 121697056 ANXA5 C 0 WILD ADP=112
chr11 46739505 F2 G 0 WILD ADP=202
chr13 20189511 GJB2 C 0 WILD ADP=326
chr13 20189547 GJB2 C 0 WILD ADP=3
答案 1 :(得分:1)
awk
救援!
$ awk 'NR==FNR{f2[$2]=f3[$3]=$4;next}
{k=$2; suf=((k in f2)?f2[k]:((k in f3)?f3[k]:"-"));
$2=k "\t" suf}1' file{1,2}
chr1 11796321 MTHFR G 0 WILD ADP=1026
chr1 169549811 F5 C 0 WILD ADP=940
chr1 173917078 SERPINC1 C 0 WILD ADP=501
chr2 48962782 FSHR C T HET ADP=1665
chr4 121696962 ANXA5 C T HET ADP=212
chr4 121697011 ANXA5 A 0 WILD ADP=184
chr4 121697037 ANXA5 T 0 WILD ADP=111
chr4 121697037 ANXA5 tccc 0 INDEL AINDEL
chr4 121697056 ANXA5 C 0 WILD ADP=112
chr11 46739505 F2 G 0 WILD ADP=202
chr13 20189511 GJB2 C 0 WILD ADP=326
chr13 20189546 GJB2 AC A INDEL ADP=164
chr13 20189547 GJB2 C 0 WILD ADP=3