匹配两个文件

时间:2017-12-22 18:07:47

标签: linux awk

我想在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

和file2(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

1 个答案:

答案 0 :(得分:0)

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