0

This question already has an answer here:

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

marked as duplicate by cuonglm, jasonwryan, chaos, Thor, Archemar Sep 22 '15 at 8:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0
!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.