Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

Rep: Reputation: Disabled
Shell Script - Getting an argument from ps result


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?


Even the output like below will also be handy:


Thanks in advance,
Old 01-09-2012, 11:33 PM   #2
Senior Member
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652
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 + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
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:

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.

Here are a few useful awk references:

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
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.


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:44 AM.

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