4 ms·
AWK makes most of that grep/cut/count-type stuff so much easier, 1-liners need something like 3 parts instead of 10. (My grep doesn't work like in the video, b
by yesenadam 8y ago
AWK makes most of that grep/cut/count-type stuff so much easier, 1-liners need something like 3 parts instead of 10.
(My grep doesn't work like in the video, but..)
for f in *.rb;do awk '$1 ~ /class|module/ {print $2}' $f;done |
awk '{a[$1]++} END{for (q in a) print a[q],q}' | sort -n
This seems to do what all the sed, cut, regrepping, wc etc do. AWK (in default mode, anyway) removes leading spaces, makes cutting and counting words easy. It took about 30 seconds to write, too.
- voltagex_ 8y agoI have never been able to get my head around awk - how did you learn it?
- FelipeCortez 8y agoThe AWK programming language book is excellent! https://archive.org/download/pdfy-MgN0H1joIoDVoIC7/The_AWK_Programming_Language.pdf https://archive.org/download/pdfy-MgN0H1joIoDVoIC7/The_AWK_P... You can find some discussion about it here: https://news.ycombinator.com/item?id=13451454 https://news.ycombinator.com/item?id=13451454
- voltagex_ 8y ago>This book was typeset in Times Roman and Courier by the authors, using an Autologic APS-5 phototypesetter and a DEC VAX 8550 running the 9th Edition of the UNIX operating system. Delightful!
- patrec 8y ago"The AWK programming language" has already been suggested and is great. But awk is actually pretty simple. Imagine everything having an implicit loop like this around it like so: # BEGIN{ ... } would go here for $0 in input.split('\n'): $1, $2, ... = line.split() # if TEST {ACTION}; pairs ... # END { ... } would go here so the first TEST in GP's awk would be `$1 ~ /class|module/` and the corresponding ACTION to take `{print $2}`. if TEST is omitted it's True, if ACTION is it's print the whole line.
- svsucculents 8y agoIt's C. Learn C and you know awk. The only awk worth using is G(nu)awk anyway.
- msla 8y agoNo, in that C doesn't have the pattern-action style of awk: /<pattern>/ { <action> } "For every line which matches <pattern> perform <action> " is the fundamental flow of awk programs. It's procedural but the iteration is hidden; it's almost like each stanza is a callback which gets executed when the pattern fires. I'm kinda surprised no "real language" has adopted this design for an API.
- svsucculents 8y agoThat's the reason I love this site. Moron downvotes and rudimentary objections.
- yesenadam 8y agoSame as usual, read every book about it![0] and don't try to learn from the man page. I think the original book by A, W and K (the 2nd edition anyway) was best. Also Arnold Robbins' books Sed and Awk and Effective AWK Programming are great. The GNU Awk User's Guide[1] is amazingly detailed, but the new AWK has bloated a lot, has 1000 features where the older one had a few dozen, and those core ones should be learnt first. (But people still like programming with it, and kept asking for features..) Hmm I don't think I use any of the new features, maybe I should... But really, it's very simple, I can't thinking of anything that's been anything like as easy to learn, except maybe BASIC. With which AWK shares some similarities - the friendliness of it, the BEGIN and END.. [1] http://www.gnu.org/software/gawk/manual/gawk.html http://www.gnu.org/software/gawk/manual/gawk.html [0] Or if there are too many, get names from lists of best books on a subject.
- yesenadam 8y agop.s. Bruce Barnett's AWK guide may be helpful, I've learned a lot from him on various UNIXy subjects. http://www.grymoire.com/Unix/Awk.html http://www.grymoire.com/Unix/Awk.html
- matt_j 8y agoEvery time I come up with some crazy bash construction someone shows me how I could have done it more elegantly with AWK and nothing else. <3 https://gregable.com/2010/09/why-you-should-know-just-little-awk.html https://gregable.com/2010/09/why-you-should-know-just-little...
- svsucculents 8y agoThat's the tip of the iceberg. You can do anything in awk. for a in *.c do awk 'function lensort(a,zerp, x,tmp) {for (x = 1 ; x < zerp ; x++) {tmp = a[x]; if (length(a[x + 1]) < length(a[x])) {a[x] = a[x + 1]; a[x + 1] = tmp}}} /^(int|void|char|struct|long|float)/ {while (x < 2) {getline ; arr[$1] = (substr($0,1,1) == "{") ? "\n" : $0 "\n" ; x++}} END {lensort(arr,v); for (c in arr) {printf "%s\n",(length(arr[c]) > 0) ? arr[c] : ""}}' ${a} done
- yesenadam 8y agoI was talking about bash one-liners, not AWK programming. p.s. You forgot to explain what your program does.