如果在file2中找到此字符串,我想在file1中为键添加星号。
谢谢,Bernardo
#file1
HPNK_00080 HPNK_00080 HPNK_04889 HAPS_1149 HAPS_1027 HAPS_0842 HPNK_04769 HAPS_1077 HPNK_10396 HPNK_04984 HPSD74_1027
HPNK_00090 HPNK_00090 HS308_01445 HAPS_0623 HAPS_0558 HS321_01221
HPNK_00277 HS312_01685 HS308_01448 HS306_01459 HPNK_00277 HS313_01643 HS310_01701 HS317_01870
#file2
HPNK_00080
HPNK_00090
HPNK_00210
#desired output
HPNK_00080* HPNK_00080 HPNK_04889 HAPS_1149 HAPS_1027 HAPS_0842 HPNK_04769 HAPS_1077 HPNK_10396 HPNK_04984 HPSD74_1027
HPNK_00090* HPNK_00090 HS308_01445 HAPS_0623 HAPS_0558 HS321_01221
HPNK_00277 HS312_01685 HS308_01448 HS306_01459 HPNK_00277 HS313_01643 HS310_01701 HS317_01870
我试过了:
awk '
BEGIN { FS=OFS="\t" }
NR==FNR { map[$1] = $2; next }
{
for (key in map)
if ($0 = key)
$0 = $0"*"
print
}
答案 0 :(得分:1)
这很接近你所拥有的。
awk 'NR==FNR{key[$1]++;next}
NR!=FNR{if ($1 in key) $1 = $1 "*"; print}' file2 file1
主要区别在于它在阅读$2
时没有引用不存在的file2
,它使用$1
的直接查找而不是循环{ {1}},在阅读$0
时查看密钥是否存在。
答案 1 :(得分:1)
假设file1中的键是唯一的,您也可以
awk '{$1 in key&&$1=$1"*";key[$1]}ARGIND==2' file2 file1