Reputation: 10284
I am trying to parse out the recent load, from the output of this command -
[sandeepan@ip-10-169-92-150 ~]$ w
14:22:21 up 17 days, 51 min, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
sandeepa pts/0 115.112.95.170 06:38 43:57 0.51s 0.51s -bash
sandeepa pts/1 115.112.95.170 13:17 4.00s 0.03s 0.00s w
The first one after the load average: -
|
\|/
load average: 0.00, 0.01, 0.05
I was using this, which worked -
w | awk '{print $10}' | tr -d ,
However, I realised that the value I am looking for may not always be the 10th variable. Hence I am trying to use regex here, but unfortunately, not able to use it with any of the few bash commands/utilities I know.
I have this regular expression, with which I am able to match the desired value -
/.*load average:\s([0-9]+\.[0-9]+),.*/m
I tried looking at the sed manual, looking at some other questions like Using sed to remove unwanted characters from output, but could not understand much to make mine working.
Upvotes: 1
Views: 54
Reputation: 289735
When you know what text comes before and after, it is best to use a look-behind that does this: checks the text in a given place, no matter what is the rest of the line.
Given your sample file, I stored it in a file and did this:
$ grep -Po '(?<=load average: )[^,]*' file
0.00
This is saying: hey, get all the text after load average:
and until a comma is found.
So if you have a file like this:
$ cat file
14:22:21 up 17 days, 51 min, 2 users, load average: 0.00, 0.01, 0.05
hello how are you, load average: 1.23, 0.01, 0.05
It will return as follows:
$ grep -Po '(?<=load average: )[^,]*' file
0.00
1.23
Note by the way that man w
can give you good hints on how to get this info with some options, as well as man top
.
Upvotes: 1