我必须按以下格式命名文件 1 和文件 2:
文件 1 有 18000 行
COL1 COL2 COL3 COL4 COL5
- . ID = tom_fa_10005086
- 0 Parent = tom_fa_10005086
- 0 Parent = tom_fa_10005086
- 2 Parent = tom_fa_10005086
- 1 Parent = tom_fa_10005086
- 0 Parent = tom_fa_10005086
- 0 Parent = tom_fa_10005086
+ . ID = tom_fa_10013928
+ 0 Parent = tom_fa_10013928
+ 0 Parent = tom_fa_10013928
+ 0 Parent = tom_fa_10013928
+ 0 Parent = tom_fa_10013928
+ 2 Parent = tom_fa_10013928
+ 2 Parent = tom_fa_10013928
+ 1 Parent = tom_fa_10013928
+ 1 Parent = tom_fa_10013928
+ 2 Parent = tom_fa_10013928
+ 2 Parent = tom_fa_10013928
- . ID = tom_fa_10000024
- 0 Parent = tom_fa_10000024
- . ID = tom_fa_10004587
- 0 Parent = tom_fa_10004587
- 1 Parent = tom_fa_10004587
- 1 Parent = tom_fa_10004587
+ . ID = tom_fa_10018753
+ 0 Parent = tom_fa_10018753
+ 2 Parent = tom_fa_10018753
+ 2 Parent = tom_fa_10018753
+ 0 Parent = tom_fa_10018753
+ . ID = tom_fa_10007441
+ 0 Parent = tom_fa_10007441
+ 0 Parent = tom_fa_10007441
+ 1 Parent = tom_fa_10007441
+ 0 Parent = tom_fa_10007441
+ 1 Parent = tom_fa_10007441
+ 2 Parent = tom_fa_10007441
和文件2
COL 1 COL 2
tom_fa_10005086 ABI3VP1
tom_fa_10013928 ABI3VP2
tom_fa_10000024 Alfin-like
tom_fa_10011338 C2C2-Dof
tom_fa_10003474 C2C2-Gata
tom_fa_10003291 C2H2
tom_fa_10007064 C2H2
tom_fa_10008972 C2H2
tom_fa_10009025 C2H2
tom_fa_10005726 C3H
tom_fa_10011317 C3H
tom_fa_10010708 CPP
tom_fa_10002138 E2F-DP
tom_fa_10009443 G2-like
tom_fa_10008970 GRAS
我想做的是,如果(文件 1)的第 5 列和第 1 列(文件 2)相等,则在文件 1 中与第 2 列(文件 2)对应的第 6 列添加
所以它看起来像
- . ID = tom_fa_10005086 ABI3VP1
- 0 Parent = tom_fa_10005086 ABI3VP1
- 0 Parent = tom_fa_10005086 ABI3VP1
- 2 Parent = tom_fa_10005086 ABI3VP1
- 1 Parent = tom_fa_10005086 ABI3VP1
- 0 Parent = tom_fa_10005086 ABI3VP1
- 0 Parent = tom_fa_10005086 ABI3VP1
+ . ID = tom_fa_10013928 ABI3VP2
+ 0 Parent = tom_fa_10013928 ABI3VP2
+ 0 Parent = tom_fa_10013928 ABI3VP2
+ 0 Parent = tom_fa_10013928 ABI3VP2
+ 0 Parent = tom_fa_10013928 ABI3VP2
+ 2 Parent = tom_fa_10013928 ABI3VP2
+ 2 Parent = tom_fa_10013928 ABI3VP2
+ 1 Parent = tom_fa_10013928 ABI3VP2
+ 1 Parent = tom_fa_10013928 ABI3VP2
+ 2 Parent = tom_fa_10013928 ABI3VP2
+ 2 Parent = tom_fa_10013928 ABI3VP2
- . ID = tom_fa_10000024 Alfin-like
- 0 Parent = tom_fa_10000024 Alfin-like
- . ID = tom_fa_10004587
- 0 Parent = tom_fa_10004587
- 1 Parent = tom_fa_10004587
- 1 Parent = tom_fa_10004587
+ . ID = tom_fa_10018753
+ 0 Parent = tom_fa_10018753
+ 2 Parent = tom_fa_10018753
+ 2 Parent = tom_fa_10018753
+ 0 Parent = tom_fa_10018753
+ . ID = tom_fa_10007441
+ 0 Parent = tom_fa_10007441
+ 0 Parent = tom_fa_10007441
+ 1 Parent = tom_fa_10007441
+ 0 Parent = tom_fa_10007441
+ 1 Parent = tom_fa_10007441
+ 2 Parent = tom_fa_10007441
我尝试使用 fgrep -f file 2 file1 但我不知道如何合并第 2 列文件 2 的信息,并且字符串不相等。
答案1
如果您想一次性完成此操作,此sed
脚本将起作用:
sed '/^[^ ]* [^ ]*$/{H;d;};G;s/ \([^ ]*\)\n.*\n\1 \([[:print:]]*\).*/ \1 \2/;P;d' file2 file1
它有什么作用?它收集file2
保留空间中的所有行作为一种查找表,然后将该表附加到模式空间的每一行,如果找到模式,则添加第六列。详细地:
/^[^ ]* [^ ]*$/
{}
匹配具有两列的行,仅对于那些,执行以下命令(里面的命令{H;d;}
将这些行追加到保留空间并停止对该行的进一步处理,因此脚本的其余部分仅针对以下行执行file1
G
将带有查找表的保留空间附加到模式空间s/ \([^ ]*\)\n.*\n\1 \([[:print:]]*\).*/ \1 \2/
是否为匹配附加第六列P
打印没有附加表的行d
抑制其他输出(选项-n
会做同样的事情)
答案2
尝试使用以下命令,效果也很好
Step1
awk 'NR==FNR{a[$5];next}($1 in a){print $0}' file1 file2 > u.txt
count=`awk '{print NR}' u.txt | sort -nr| sed -n '1p'`
step2
for ((i=1;i<=$count;i++)); do h=`awk -v i="$i" 'NR==i{print $1}' u.txt`; j=`awk -v i="$i" 'NR==i{print $2}' u.txt `; awk -v h="$h" -v j="$j" '$5 == h {$6=j;print $0}' file1; done >file3.txt
step3
awk 'NR==FNR{a[$1];next}!($5 in a){print $0}' file2 file1 >file4.txt
Step4(Final output)
awk '{print $0}' file3.txt file4.txt
- . ID = tom_fa_10005086 ABI3VP1
- 0 Parent = tom_fa_10005086 ABI3VP1
- 0 Parent = tom_fa_10005086 ABI3VP1
- 2 Parent = tom_fa_10005086 ABI3VP1
- 1 Parent = tom_fa_10005086 ABI3VP1
- 0 Parent = tom_fa_10005086 ABI3VP1
- 0 Parent = tom_fa_10005086 ABI3VP1
+ . ID = tom_fa_10013928 ABI3VP2
+ 0 Parent = tom_fa_10013928 ABI3VP2
+ 0 Parent = tom_fa_10013928 ABI3VP2
+ 0 Parent = tom_fa_10013928 ABI3VP2
+ 0 Parent = tom_fa_10013928 ABI3VP2
+ 2 Parent = tom_fa_10013928 ABI3VP2
+ 2 Parent = tom_fa_10013928 ABI3VP2
+ 1 Parent = tom_fa_10013928 ABI3VP2
+ 1 Parent = tom_fa_10013928 ABI3VP2
+ 2 Parent = tom_fa_10013928 ABI3VP2
+ 2 Parent = tom_fa_10013928 ABI3VP2
- . ID = tom_fa_10000024 Alfin-like
- 0 Parent = tom_fa_10000024 Alfin-like
- . ID = tom_fa_10004587
- 0 Parent = tom_fa_10004587
- 1 Parent = tom_fa_10004587
- 1 Parent = tom_fa_10004587
+ . ID = tom_fa_10018753
+ 0 Parent = tom_fa_10018753
+ 2 Parent = tom_fa_10018753
+ 2 Parent = tom_fa_10018753
+ 0 Parent = tom_fa_10018753
+ . ID = tom_fa_10007441
+ 0 Parent = tom_fa_10007441
+ 0 Parent = tom_fa_10007441
+ 1 Parent = tom_fa_10007441
+ 0 Parent = tom_fa_10007441
+ 1 Parent = tom_fa_10007441
+ 2 Parent = tom_fa_10007441