Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

unix - Explain this duplicate line removing, order retaining, one-line AWK command

I learned a really handy way to remove duplicate lines retaining the order from Remove duplicates without sorting file - BASH.

Say, if you have the following file,

$cat file
a
a
b
b
a
c

you can use the following to remove the duplicate lines:

$awk '!x[$1]++' file
a
b
c

How does this work in terms of precedence of operations?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The expression is parsed as

!(x[$(1)]++)

So, from the inside out, it's:

  • Take field 1 of the current input line, $(1) (note that $ is an operator in AWK, unlike in Perl).
  • Index x with the value of field 1; if x is an unbound variable, bind it to a new associative array.
  • Post-increment x[$(1)]; a rule similar to the one in C applies, so the value of the expression is that of x[$(1)] prior to the increment, which will be zero if x[$(1)] has not yet been assigned a value.
  • Negate the value of the previous, which will yield truth when x[$(1)] is zero.
  • Actually do the increment so that x[$(1)] gets a non-zero value. So, the next time, x[$(1)] for the same value of $(1) will return 1.

This expression is then evaluated for every line in the input and determines whether the implied default action of awk should be executed, which is to echo the line to stdout.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...