Quote:
Originally Posted by mecano
[...]
if I understand correctly, awk reads the two files and automagically merged records itself ? It means that there is no need to store values from file1 to compare them to file2 ? Beautifull...
[...]
|
It uses an associative array (a hash), so it guarantees the uniqueness
of the key ($NF in this case) and the value is alway the last one it sees
(the one in file2). It will associate to every key ($NF) the entire record ($0) and it will update the value when it sees the same key.
Quote:
|
Two things I don't get: the use of the underscore (while i guess it stands for "all read records" ?), and why is END not at the end ?
|
Well, this is kinda style of writing,
it you want the code more readable,
you could use this instead (and this is compatible even with the old plain Solaris awk):
Code:
awk '{
key_record[$NF] = $0 # associate key ($NF) with entire record ($0)
}
END {
# after the entire input has been read
for (key in key_record) # for every key stored
print key_record[key] # print the associated value
}' file1 file2