Profile photo for Ronald Loui

Here is an example where a slightly less terse version might be better, from the point of view of instruction, if not also readability and explicitness. Certainly for awk newbies, there is a lot going on that is "magic shortcut" that actually makes awk harder to learn than necessary in the script given:

Your

awk '!x[$0]++'

is actually a shorthand for

awk '!x[$0]++ { print }'

which is short for

awk '!x[$0]++ { print $0 }'

which is short for

awk '{ if (!x[$0]) print $0; x[$0]++ }'

which could be written

awk ' !seen[$0] { print $0 } 1 { seen[$0]++ }'

or

awk '{ if (!seen[$0]++) print }'

or even

awk '!($0 in seen) { out[++n]=$0; seen[$0] } END { for (i=1; i<=n; i++) print out[i] }'

or even

awk 'BEGIN { while (getline s) { if (!seen[s]) print s; seen[s]=1 } }'

which still requires explanation of associative arrays and implicit variables and file handlers. But the last form will get the person who likes c-family languages to understand awk immediately. From that basic understanding, the shortcuts and streaming pattern/action style can easily be acquired.

Awk people who are in love with the shorthands do the language no favors when it comes to helping a broader base of programmers to learn and appreciate it.

View 3 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025