LinuxQuestions.org
Register a domain and help support LQ
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 09-27-2010, 03:21 AM   #1
Damarr
LQ Newbie
 
Registered: May 2010
Posts: 15

Rep: Reputation: 0
Scripting: formatting ps output w/Perl?


Hello

I'm writing a script that gives me some pertinent info about my servers, and the last little piece I need to figure out is the process list. There are going to be non-technical people looking at the output of the script and I'm trying to make it as simple as possible.

Right now, if I do ps -ef | grep <process> | grep -v grep
It obviously shows me the process, but the problem is that there is 4 lines of info in front of the process. ie

root PID date time java -1024 -cp oh,my,god,there,is,so,much,data,it,carries,on,for,4,lines,I,want,to,shoot,myself,trying,to,read,it,f inally,in,the,end,there,is,a,space /here/is/the/process

I'd like the output to be something like this:

PID: #### Process: /here/is/the/process

I've thought about how I could take the output and use awk or sed but I know there is probably an easier way to do it with perl.

Can anyone give me a hint on the direction I should be looking?

Thanks!

Damarr
 
Old 09-27-2010, 03:47 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
How about:
Code:
ps -eo pid,cmd
 
Old 09-27-2010, 05:12 AM   #3
Damarr
LQ Newbie
 
Registered: May 2010
Posts: 15

Original Poster
Rep: Reputation: 0
Thats close but there is still a lot of info I want to cut out from the "cmd"
 
Old 09-27-2010, 05:53 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well have a look through the man pages as I only had a quick glance.
 
Old 09-27-2010, 05:58 AM   #5
Damarr
LQ Newbie
 
Registered: May 2010
Posts: 15

Original Poster
Rep: Reputation: 0
I am, I am....however the command itself is very large. I'm trying now to find a way to show only the last part of the command
 
Old 09-27-2010, 06:07 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Quote:
I'm trying now to find a way to show only the last part of the command
Last part of what??
For example:
Code:
grail     2418  2106  0 19:18 ?        00:00:00 python /usr/share/system-config-printer/applet.py
Showing the last part here would get you the file being run but Python is the actual command??
 
Old 09-27-2010, 06:18 AM   #7
Damarr
LQ Newbie
 
Registered: May 2010
Posts: 15

Original Poster
Rep: Reputation: 0
when I type the command you gave me above this is what I get:

12345 java -Xmx1024m -cp /directory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:
/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:
/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:
/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:/anotherdirectory/file.jar:
/anotherdirectory/file.jar:/anotherdirectory/file.jar /this/is/the/part/I/want.properties

So, all I want is the last little bit where there is a space between the last mentioned .jar and the file.
 
Old 09-27-2010, 06:23 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
In that case I would recommend awk:
Code:
ps -eo pid,cmd | awk '{print $1,$NF}'
 
Old 09-27-2010, 06:27 AM   #9
Damarr
LQ Newbie
 
Registered: May 2010
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks a ton that last bit was what I needed.

Code:
ps -eo pid,cmd | grep <process> | grep -v grep | awk '{print $1,$NF}'
 
Old 09-27-2010, 06:31 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
How about we just use one application to do the work
Code:
ps -eo pid,cmd | awk '/<process>/ && !/awk/{print $1,$NF}'
 
Old 09-27-2010, 07:11 AM   #11
Damarr
LQ Newbie
 
Registered: May 2010
Posts: 15

Original Poster
Rep: Reputation: 0
what would be the equivalent of "|grep -v /dev/null" for awk? There are a couple of processes coming up as

PID /dev/null
 
Old 09-27-2010, 07:20 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Code:
&& !"/dev/null"
 
Old 09-27-2010, 07:24 AM   #13
Damarr
LQ Newbie
 
Registered: May 2010
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks!
 
  


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
BASH scripting: problem with file formatting. suse_nerd Programming 4 12-07-2009 05:31 PM
How to redirect the output of exec command output in perl? sudhap85 Linux - Newbie 3 08-28-2009 04:35 PM
file formatting via shell scripting athreyavc Programming 2 10-09-2007 04:55 AM
Formatting output krock923 Programming 2 06-07-2006 08:26 AM
Formatting output of ls doodar Linux - Newbie 29 07-29-2004 02:25 PM


All times are GMT -5. The time now is 08:39 AM.

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