I'm trying to grab some measurements on a per process level in this script I'm writing. The easiest way to see the values I'm looking for is to just grab the output of the top
command.
So when I try to parse it though, my regex looks kind of ridiculous. Given this output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8364 cgroup_t 20 0 764m 646m 1520 R 101.7 4.3 0:05.51 perl
I came up with the regex to grab some values(the 8364 is passed in on a var and shown here for ease of reading and the top output is stored on a var called $top_string):
if($top_string =~ m/^\s*8364\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/){
#return desired var number, ie. $1,$2...etc
}
This works but it seems like overkill. Is there any way to do this more efficiently? I feel like maybe I remember a way to avoid typing the \s+([^\s]+)
pattern over and over.
Anyway thanks for taking the time to read this!
Cheers