Sometimes it is useful to compare two files. To do this in awk, the trick is to load the data from the first file into an array.
Let's say for instance we have a list of first names in file1, one per line:
John Mary
and in file2 with complete names:
John Smith Mark Fo Mary Bar
We want to find the names in file2 corresponding to the first name in file1. This can be done in a compact manner like this:
awk 'FNR==NR {arr[0ドル];next} 1ドル in arr' file1 file2
Some explanations:
Note: For this example, join(1) is a working alternative.
Powered by sdf Creative Commons License <pierre.gaston+awk@gmail.com>
Awk Wiki by #awk is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.