Reputation: 1
I am new using AWK. I would thank you if you could help me.
I have two files
file1
1 2 3 4 ok I
5 36 5 6 no II
58 9 6 4 no V
4 3 2 1 ok III
file2
6 9 8 6
58 6 5 4
4 3 2 1
1 2 3 4
I want to find each row from file2 in file 1 and if it matches print columns 5 and 6 from file1
Output expected
ok III
ok I
Upvotes: 0
Views: 183
Reputation: 204477
$ cat tst.awk
{ key = $1 FS $2 FS $3 FS $4 }
NR == FNR {
map[key] = $5 OFS $6
next
}
key in map { print map[key] }
.
$ awk -f tst.awk file1 file2
ok III
ok I
Upvotes: 1
Reputation: 40773
My solution is to create a file call match.awk:
FNR == NR {
found[$1 "/" $2 "/" $3 "/" $4] = 1
}
FNR != NR && $1 "/" $2 "/" $3 "/" $4 in found {
print $5, $6
}
Invoke it:
awk -f match.awk file2 file1
Discussion
FNR == NR
58 6 5 4
, then the key will be 58/6/5/4
. With this key, we mark the array found
with a 1 (true)Upvotes: 1