7 Answers
7

Can give wrong results if any of the field values or names contain the string "rating", i.e. one of the session names has the word "rating" in it. Better: awk -F: '$1 == "rating" {print $2}'
–
Ingmar HuppOct 4 '11 at 0:39

(I don't know what you're doing with localhost above, so used your output as input to my awk command, then replaced the "cat" with "nc ..." - but it should be fine.)

But why would you do this? The UNIX way is to have lots of small tools, each of which does one thing well, strung together via pipelines, rather than using larger multifunction tools. You need to select a single matching line, the select one field therefrom: grep selects lines with matches, and cut selects fields from input lines; they're perfect for the task. But if you really do want to do it all-in-one with awk, well, there you go.

One reason is that using grep and cut requires spawning two processes, and using awk (or sed) requires only one. Spawning a process is computationally expensive. Also, unlike using perl or ruby (et al), sed and awk are much more lightweight up front.
–
MeiOct 3 '11 at 15:48

2

Fair enough, though I note that the size of the awk binary on my (F15) system is 360948 bytes, while the grep and cut binaries together are 170824. So less cycles with only one fork+exec, but more memory, and more IO to get the binary off disc. Honestly, I doubt that any of these considerations matter on modern systems, but if you're minded to minimise, there you go!
–
MadHatterOct 3 '11 at 16:46

Foo Bah: thanks for your edit proposal, but quanta made a later post than mine which is even more streamlined than your suggestion. I preferred mine because it's easier for an entry-level awk user to see how it works, and therefore rejected your edit. Thank you for the thought, though!
–
MadHatterOct 3 '11 at 16:50