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.

posted @ 2019-02-01 16:49  泽锦  阅读(486)  评论(0编辑  收藏  举报