LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell Script - Getting an argument from ps result (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-getting-an-argument-from-ps-result-922962/)

sajjadshahid 01-09-2012 09:24 PM

Shell Script - Getting an argument from ps result
 
Hi,

I am writing a shell script, and want to get a specific argument from 'ps -ef' results. The order of arguments is different and awk (with column no) gives different values.

ps -ef | grep process
returns multiple processes with more than 50/60 unordered arguments each, but each process has a unique argument e.g. -Dwas.servername=WP_1

How can I 'ps -ef' so that I get values for -Dwas.servername for each process like below?

WP_1
WP_2
WP_3
...

Even the output like below will also be handy:

-Dwas.servername=WP_1
-Dwas.servername=WP_2
-Dwas.servername=WP_3
...

Thanks in advance,

kbp 01-09-2012 10:33 PM

Code:

ps -ef <pattern> | perl -pi -e 's/.*was\.servername=(\w).*/\1/g'

David the H. 01-10-2012 12:33 AM

Please try doing some searches of the archives and the web before you post your questions. This kind of thing comes up very often. In fact, I posted this for another user just a few days ago:

http://www.linuxquestions.org/questi...n-bash-921210/

sed and awk are the workhorse tools to use for extracting substrings from text. A sed command would look almost identical to the perl solution posted above, in fact.

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

Also, as I mentioned in that previous thread, have a look at the man page for ps. It has a large number of options for formatting the output, and could simplify your requirements.

sajjadshahid 01-10-2012 01:10 AM

Thanks, Got the required output.
 
Hi, Thanks a lot, kbp. I got the desired output.
Thank you David also for sharing useful resources.


All times are GMT -5. The time now is 01:23 AM.