Yesterday I was googling how to merge two files and came across an awk snippet.

I need a simple merge, so sort -u is not the way to go, but the code below works.

Could some one please explain what this awk code does?

awk '!a[$0]++' file_1 file_2
!a[$0]++      Remove duplicate, nonconsecutive line

This is an awk one-liner. It registers the lines seen in the associative-array "a" (arrays are always associative in Awk) and at the same time tests if it had seen the line before. If it had seen the line before, then a[line] > 0 and !a[line] == 0.

Any expression that evaluates to false is a no-op, and any expression that evaluatess to true is equal to "{ print }". an empty statement "a[$0]" creates an element in the array.

for more on awk check this.

Not the answer you're looking for? Browse other questions tagged or ask your own question.