awk按顺序去除重复行

# root @ c7bit1 in ~ [16:43:40] 
$ cat test
a
b
c
d
b
g


# root @ c7bit1 in ~ [16:46:27] C:2
$ awk '!x[$0]++'  test
a
b
c
d
g


解释:

a[$0]: look at the value of key $0, in associative array a. If it does not exist, create it.

a[$0]++: increment the value of a[$0], return the old value as value of expression. If a[$0] does not exist, return 0 and increment a[$0] to 1 (++ operator returns numeric value).

!a[$0]++: negate the value of expression. If a[$0]++ return 0, the whole expression is evaluated to true, make awk performed default action print $0. Otherwise, the whole expression is evaluated to false, causes awk do nothing.

原文地址:https://www.cnblogs.com/zejin2008/p/10346312.html