LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2012, 10:24 PM   #1
sajjadshahid
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Rep: Reputation: Disabled
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,
 
Old 01-09-2012, 11:33 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
Code:
ps -ef <pattern> | perl -pi -e 's/.*was\.servername=(\w).*/\1/g'
 
1 members found this post helpful.
Old 01-10-2012, 01:33 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
1 members found this post helpful.
Old 01-10-2012, 02:10 AM   #4
sajjadshahid
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks, Got the required output.

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
execute script shell with regex as argument eguider Linux - Newbie 2 12-15-2009 08:26 PM
Formatting result file using Shell script athreyavc Programming 3 09-22-2008 06:57 AM
Shell script to cd into the result of Find odinsride Programming 5 10-17-2007 11:33 AM
shell script, Passing string as argument? antis Programming 4 09-07-2007 02:03 AM
PHP pass argument to shell script monzter Programming 2 08-14-2004 07:16 AM


All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration