Am an AWK newbie, using GNU utilities ported to Windows (UNXUtils) and gawk instead of awk. A solution on this forum worked like absolute magic, and I'm trying to find a source I can read to understand better the pattern expression offered in that solution.
In Select unique or distinct values from a list in UNIX shell script an answer by Dimitre Radoulov offering the following code
zsh-4.3.9[t]% awk '!_[$0]++' file
as a solution for selecting elements of a list with repeated and jumbled elements, listing each element only once.
I had previously used sort | uniq
to do this, which worked fine for small test files. For my actual problem (extracting the list of company symbols from archival order book research data from India's National Stock Exchange for 16 days in April 2006, with 129+ million records in multiple files), the sorting burden became too much. And uniq only eliminates adjacent duplicates.
Copying the above line for my Win-GNU gawk, I used
C:\Users\PAPERS\> cat ..\Full*_Symbols.txt | gawk "!_[$0]++" | wc -l
946
suggesting that the 129+ million records pertained to 946 different firms, which is a VERY reasonable answer. And it took under 5 minutes on my modest Windows machine, after hours of trying to SORT wore me out.
Looked at all the awk texts I have and searched a bit online, and while for part of the pattern the explanation of why it worked is clear (!
serves as NOT, $0
is the whole current record), for the underscore _
I am not able to find any explanation, and have seen ++
in examples only as "update the counter by 1."
Will be grateful for any appropriate text or web reference to understand this example fully, as I think it will help me in other related cases as well. Thanks. Best,