[英]bash - Find names in file1 according to IDs in file2
Sorry if this is duplicate, but I searched and haven't found exactly same question. 抱歉,如果这是重复的,但是我搜索了却没有发现完全相同的问题。 So I have
所以我有
File1: 文件1:
Aaron ID00456 亚伦ID00456
Brad ID00123 布拉德ID00123
Cassie ID00789 卡西ID00789
Doug ID12345 道格ID12345
Ethan ID05555 伊桑ID05555
File2: 文件2:
ID12345 ID12345
ID00123 编号
ID00456 ID00456
Keeping the order of IDs in File2, I'd like to have output File3 as: 保持ID在File2中的顺序,我希望将File3输出为:
Doug ID12345 道格ID12345
Brad ID00123 布拉德ID00123
Aaron ID00456 亚伦ID00456
Try this script (suppose File1.txt and File2.txt are in the same directory of the script). 尝试使用此脚本(假设File1.txt和File2.txt位于脚本的同一目录中)。
#!/bin/bash
while read -r ID2
do
while read -r NAME ID1
do
if [ "$ID1" = "$ID2" ]
then
echo $NAME $ID1 >> File3.txt
fi
done < File1.txt
done < File2.txt
Then find File3.txt in the same directory with the content: 然后在与内容相同的目录中找到File3.txt:
Doug ID12345
Brad ID00123
Aaron ID00456
awk
to the rescue! awk
解救!
$ awk 'NR==FNR {a[$2]=$1; next}
{print a[$1],$1}' file1 file2
Doug ID12345
Brad ID00123
Aaron ID00456
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.