News for hackers. Stuff that matters.
Re:Problem solving with Unix commands by yesenadam on Thursday February 14, 2019 @05:17PM (#19166608)
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.
Re:Problem solving with Unix commands by matt_j on Thursday February 14, 2019 @07:07PM (#19167324)
Every 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...

Re:Problem solving with Unix commands by svsucculents on Saturday February 16, 2019 @12:19PM (#19179625)
That'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

Re:Problem solving with Unix commands by yesenadam on Saturday February 16, 2019 @09:25PM (#19182274)
I was talking about bash one-liners, not AWK programming. p.s. You forgot to explain what your program does.
Re:Problem solving with Unix commands by voltagex_ on Thursday February 14, 2019 @06:28PM (#19167090)
I have never been able to get my head around awk - how did you learn it?
Re:Problem solving with Unix commands by yesenadam on Saturday February 16, 2019 @10:10PM (#19182469)
Same 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

[0] Or if there are too many, get names from lists of best books on a subject.

Re:Problem solving with Unix commands by yesenadam on Monday February 18, 2019 @12:24AM (#19188805)
p.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

Re:Problem solving with Unix commands by FelipeCortez on Thursday February 14, 2019 @06:32PM (#19167114)
The AWK programming language book is excellent!

https://archive.org/download/pdfy-MgN0H1joIoDVoIC7/The_AWK_P...

You can find some discussion about it here: https://news.ycombinator.com/item?id=13451454

Re:Problem solving with Unix commands by voltagex_ on Friday February 15, 2019 @12:00AM (#19168559)
>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!

Re:Problem solving with Unix commands by patrec on Thursday February 14, 2019 @06:52PM (#19167233)
"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.

Re:Problem solving with Unix commands by svsucculents on Saturday February 16, 2019 @11:33AM (#19179363)
It's C. Learn C and you know awk. The only awk worth using is G(nu)awk anyway.
Re:Problem solving with Unix commands by msla on Saturday February 16, 2019 @08:19PM (#19182017)
No, 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.