0

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
cuonglm
  • 153,898

1 Answers1

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.

FargolK
  • 1,667