LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting: formatting ps output w/Perl? (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-formatting-ps-output-w-perl-834681/)

Damarr 09-27-2010 02:21 AM

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

grail 09-27-2010 02:47 AM

How about:
Code:

ps -eo pid,cmd

Damarr 09-27-2010 04:12 AM

Thats close :) but there is still a lot of info I want to cut out from the "cmd"

grail 09-27-2010 04:53 AM

Well have a look through the man pages as I only had a quick glance.

Damarr 09-27-2010 04:58 AM

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 :)

grail 09-27-2010 05:07 AM

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

Damarr 09-27-2010 05:18 AM

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.

grail 09-27-2010 05:23 AM

In that case I would recommend awk:
Code:

ps -eo pid,cmd | awk '{print $1,$NF}'

Damarr 09-27-2010 05:27 AM

Thanks a ton :) that last bit was what I needed.

Code:

ps -eo pid,cmd | grep <process> | grep -v grep | awk '{print $1,$NF}'

grail 09-27-2010 05:31 AM

How about we just use one application to do the work :)
Code:

ps -eo pid,cmd | awk '/<process>/ && !/awk/{print $1,$NF}'

Damarr 09-27-2010 06:11 AM

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

PID /dev/null

grail 09-27-2010 06:20 AM

Code:

&& !"/dev/null"

Damarr 09-27-2010 06:24 AM

Thanks!


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