4 ms·
And here is the ungolfed version: awk '{ if (! visited[$0]) { print $0; visited[$0] = 1 } }'
by augustk 7y ago
And here is the ungolfed version:
awk '{ if (! visited[$0]) { print $0; visited[$0] = 1 } }'
- zufallsheld 7y agoMuch more readable and understandable.
- RBerenguel 7y agoYou can always write AWK in a file and read the script with -f, making it fully readable (and AWK is quite a pleasantly readable and surprisingly versatile language to write at that point)
- asicsp 7y agoto add to this, if you've coded one-liner first, you can convert to script using -o option for ex: awk -o '{ORS = NR%2 ? " " : RS} 1' gives (default output file is awkprof.out) { ORS = (NR % 2 ? " " : RS) } 1 { print $0 }
- nerdponx 7y agoThat I did not know, great tip, thank you!
- RBerenguel 7y agoI wasn't aware of this, might come handy for "one liner edge cases".
- augustk 7y agoOr maybe better: awk '! visited[$0] { print $0; visited[$0] = 1 }'
- asicsp 7y agoyet another version: awk '!($0 in seen); {seen[$0]}'
- deleted 7y ago[deleted]
- mtzet 7y agoYes. Examples like the one linked make it appear that awk is write-only language, which it really isn't. 'awk' is really a very beautiful little language. It's concise enough to solve many tasks in a single line, making it easy to use interactively while still being able to grow to moderately-sized scripts. It's not supposed to replace a full-blown scripting language like Python, but for processing files line-by-line it's superb.