Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing the output of ps command

Status
Not open for further replies.

new2unix

Programmer
Feb 5, 2001
143
US
Hi,

I am writing a script to issue a Unix ps command and with some grep to give a sample output such as this:

application_name -m some_value -p some_value -C some_value -d 0 -c 0

The value I am really after is the some_value after "-p". What would be the best way to extract the value right after the "-p" parameter assuming the "-p" may appear any where (but just once) after the application_name?


Thanks

Mike
 
Code:
$input_line =~ /-p\s(.*)\s-C/;
$some_value = $1;[\code]

and then $1 will be your "some value" --Derek

[i]"Fear not the storm for this is where we grow strong."[/i]
 
Derek,

Thanks! I am off to a good start. But just in case, should I skip the part on checking for "-C" at the end? Since the "-p" may appear any where in the output line, it may show up after "-C".

Mike
 
Ah yes....
Then I suggest a slight modification:

$input_line =~ /-p\s([\s^-])/;

I think this will make sure to stop before the next dash or space. The way it was before, it would grab the rest of the line without the -C. --Derek

"Fear not the storm for this is where we grow strong."
 
oops, dang it move the ^ to before the \s --Derek

"Fear not the storm for this is where we grow strong."
 
Hi Derek,

This is what I ended up using based on your suggestion:

$port =~ /-p.(\d+)/;
$port = $1;

I was able to get the value specifically associated with "-p". Not sure if there is an another way to accomplish the same on one single statement. But, this would do. :)

Thanks

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top