Skip to content
Back to posts

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 1
b 2
c 3

File b contains:

a 5
b 6

The desired result is:

a 1 5
b 2 6
c 3

The awk command is:

awk '{a[$1]=a[$1]" "$2}END{for (j in a) print j""a[j]}' a b

Honestly, 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 b

The result, with two spaces between values, is:

1 5
2 6
3

Another approach is:

Terminal window
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 6
a 5

Originally published on SegmentFault.

Copyright notice

Unless otherwise stated, quietin owns the copyright in all articles. All rights reserved. Except as permitted by law or with prior written permission, reproduction or republication, in whole or in part, is prohibited. Permitted quotations must credit the author and link to the original article.