gpt4 book ai didi

linux - 匹配两个文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:59 24 4
gpt4 key购买 nike

我想在 Linux 上对两个文件进行匹配

我有两个文件:

File1(3个字段,用|分隔)

M000990520|CAMPOS HORMAZA MIGUEL | 1,725.30
M000990521|MATEO MIGUEL ZENON | 1,143.86
M000990522|HAITI MIRAFLORES | 1,054.81
M000990523|MESIAS FIBIA ELESCANO DE | 826.33
M000990525|DISTRIBUIDORA LUZA S R LTDA DILU | 985.87
M000990526|ZARATE DAVILA GREGORIO | 1,513.10

和文件 2(1 个字段):

M000990521
M000990523
M010990521
M002990523
M004990521
M004990523

我需要匹配并得到这个:

M000990521|MATEO MIGUEL ZENON | 1,143.86
M000990523|MESIAS FIBIA ELESCANO DE | 826.33

我尝试使用这个 awk 命令:

awk 'FNR==NR{a[$1];next}($1 in a){print}' file1 file2

最佳答案

awk -F'|' 'NR==FNR{A[$1];next}$1 in A' file2 file1

M000990521|MATEO MIGUEL ZENON | 1,143.86
M000990523|MESIAS FIBIA ELESCANO DE | 826.33

grep -wFf file2 file1

M000990521|MATEO MIGUEL ZENON | 1,143.86
M000990523|MESIAS FIBIA ELESCANO DE | 826.33

awk -F'|' 'NR==FNR{c[$1]++;next};c[$1] > 0' file2.txt file1.txt

M000990521|MATEO MIGUEL ZENON | 1,143.86
M000990523|MESIAS FIBIA ELESCANO DE | 826.33

cut -d'|' -f 1 file2.txt | while read pat; do grep "^$pat" file1.txt; done

M000990521|MATEO MIGUEL ZENON | 1,143.86
M000990523|MESIAS FIBIA ELESCANO DE | 826.33

关于linux - 匹配两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47945629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com