Je veux faire correspondre les colonnes 1 et 2 du fichier for_matching avec les fichiers des différents répertoires du chemin et nommer / .file et imprimer la ligne entière correspondant à ces colonnes
/ .file (exemple)
carrot 124555 1 2 6 hair 9922 2 2 2 tree 2223 2 1 2
for_matching
carrot 124555
sortie
carrot 124555 1 2 6
En ce moment, je peux simplement faire correspondre la colonne 1 entre les deux.
for i in */*.file; do awk -F, 'FNR==NR {a[$1]=$0; next}; $1 in a {print a[$1]}' $i for_matching > $i.matched; done
Utiliser awk
awk 'FNR==NR{arr[$1,$2];next}(($1,$2) in arr)' for_matching file
Résultats de test:
$ cat file carrot 124555 1 2 6 hair 9922 2 2 2 tree 2223 2 1 2 $ cat for_matching carrot 124555 $ awk 'FNR==NR{arr[$1,$2];next}(($1,$2) in arr)' for_matching file carrot 124555 1 2 6
De même avec plusieurs fichiers, pas besoin de ls */*.file
#!/usr/bin/env bash for i in */*.file; do awk 'FNR==NR{arr[$1,$2];next}(($1,$2) in arr)' for_matching "$i" >"$i.matched" done
C’est si simple que vous pourriez juste:
$ grep -F -w -f for_matching file carrot 124555 1 2 6
Pour une limitation, voir le commentaire de @karakfa ci-dessous.
Cela pourrait bien sûr être contourné avec (:
$ cat file carrot 124555 1 2 6 1 carrot 124555 1 2 6 $ grep -w -f <(sed 's/^/^/g' for_matching) file carrot 124555 1 2 6