I would like to get the number from rating
as output from this
# nc localhost 9571
language:
language:en_ZA.UTF-8
language:en_ZW.UTF-8
session-with-name:Ubuntu Classic (No effects):gnome-session --session=2d-gnome
session-with-name:Ubuntu (Safe Mode):gnome-session -f --session=2d-gnome
session-with-name:Ubuntu Classic:gnome-session --session=classic-gnome
xsession:/etc/X11/Xsession
rating:94
I can do it like this
# nc localhost 9571 | grep rating | cut -d: -f2
94
but could awk
be used instead for a simpler solution?
Quanta beat me to it, but I'll include a
sed
variant if you're that way inclined:Ditto what MadHatter said, though. Your current solution is perfectly sound. (Although I'd grep for
"^rating:"
rather than just the word to ensure you only get the line you want.)You can also just use the shell:
yes, you can (and should) use (one) awk instead of (two) grep and cut:
Be sure to match you line as good as you can to avoid ugly bugs.
/rating/
works,/^rating/
is better (safer),/^rating:/
is best (in this case).It can:
(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, andcut
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.While quanta's answer is the easiest and the one I would write too, I bet you didn't know GNU awk (gawk) can do networking too? You can write the entire thing with awk if you really want:
This might be useful if a server doesn't have nc installed, nc has restricted perms, or if you just really like awk.
You can also use sed,
nc localhost 9571 | sed -n "s/^rating:\([\d]*\)/\1/p"
This will make sure that "rating" starts the line, and that digits follow the
rating:
If Perl is an option:
nc localhost 9571 | perl -F: -lane 'print $F[1] if /rating/'
-a
autosplits each line into the@F
array-F:
uses:
as the field separator, instead of the default of whitespace/rating/
returns true if the regex is found$F[1]
is the 2nd element of the@F
array.Perl arrays start with element 0, whereas awk starts with $1