Joining Two Files Line by Line with awk
An awk associative-array technique for matching the first field and joining corresponding records from two files.
Suppose two files need to be joined line by line.
File a contains:
a 1b 2c 3File b contains:
a 5b 6The desired result is:
a 1 5b 2 6c 3The awk command is:
awk '{a[$1]=a[$1]" "$2}END{for (j in a) print j""a[j]}' a bHonestly, this line is difficult to understand. Even after reading the awk syntax, I had not encountered this particular pattern. A Google search led to an explanation:
{ a[$1]=a[$1]" " $2; next }appends the second field ($2) to an array (a) indexed by the first field ($1).
In other words, the array a uses $1 as its key. The initial value of a[$1] is an empty string, and each matching " "$2 value is appended to it.
For example:
awk '{a[$1]=a[$1]" "$2}END{for (j in a) print a[j]}' a bThe result, with two spaces between values, is:
1 5 2 6 3Another approach is:
paste -d' ' a b | awk '{print $1, $2, $4}'The first solution is better because it still joins the records correctly when the first column in file b is in a different order. For example, the result remains the same when b contains:
b 6a 5