Report running processes in a specific format (tricky column filtering)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Report running processes in a specific format (tricky column filtering)
# 1  
Old 02-14-2008
Report running processes in a specific format (tricky column filtering)

First off I have HP Unix, so as far as I can tell the PS command does not let me list specifically which columns I would like or what order I would like to see them.

I want to make a custom PS script for checking what SAS process are running. I want it to take 1 argument of user name and only report processes for that user. I also only want to report process that start with /sas/sas_8.2/sas. What I want to report for these processes is the last argument in there command statement (the count09.sas, the program being executed by sas) along with the ProcID, STIME and TIME. I'm having trouble parsing the PS statement, for one reason, because the amount of "columns" That awk sees before the command statement varies depending on the values in each column.

Example
say you get the following output
Code:
$ps -efx|grep user023 
 user023   207 29975  0 09:21:16 pts/8     0:00 /bin/bash
 user023 11512   115  3 15:33:09 pts/7     0:00 ps -efx
 user023 29975 29974  0 09:20:17 ?         0:01 s
 user023 29977 29975  0 09:20:17 pts/5     0:00 /bin/bash
    root 28948  3519  0 09:12:16 ?         2:04 sshd: user023@pts/3
 user023 11513   115  0 15:33:09 pts/7     0:00 grep user023
 user023 19003     1  0  Feb  7  ?         0:00 /sas/sas_8.2/sas -autoexec /sas/autoexec_generic.sas -work /wk01/saswork count09.sas
 user023 10863     1 252 15:24:07 pts/7     5:38 /sas/sas_8.2/sas -autoexec /sas/autoexec_generic.sas -work /wk01/saswork postpull06rev.sas
 user023   115 29975  0 09:20:40 pts/7     0:00 /bin/bash

I would like to be able to type
Code:
$p user023
Project Name          ProcID    Start         Time
postpull06rev.sas     10863     15:24:07      5:38
count09.sas           19003     Feb  7        0:00

My attempt so far only lists the running project names:
Code:
$cat ~/bin/p
#!/usr/bin/bash
ps -efxa|sed -n 's/user023.*saswork//p'|grep .sas

and has a hardcoded user name.

Any ideas on an approach here? Awk columns has been falling short for me, and my script is still significantly lacking. Is there any way to do column management in my output script so that the titles line up with each column as well?

To explain why awk columns won't work for me take the following output:
Code:
$ ps -u user023 -xf|grep /sas|awk '{ print $13 "\t" $2 "\t" $5 "\t" $7 }'
postpull10rev.sas       8566    10:02:58 9:16
        10905   10:24:15 0:00
/wk01/saswork   19003   Feb ?

See how postpull10rev.sas is the 13th column? but count09.sas is the 14th column? This is because STIME reads "10:02:58" in one and "FEB 13" in the other.

Thanks,
GoldFish
# 2  
Old 02-14-2008
Check out the "-o" option of ps.

e.g. ps -u<user> -o"pid,stime"
# 3  
Old 02-14-2008
I've tried this previously. I get "ps: illegal option -- o". My man page says that "-o" is XPG4 Only. Several other options are labeled XPG4 only and are unusable as well.

It seems really odd to me that -o wouldn't work, because as far as I can tell, this makes the availabilty of columns like etime useless, because it doesn't come in any of the pre established lists like -l or -f, so I have no way of displaying it.

To give you a better idea of what I do and don't have available here is my usage:
Code:
usage: ps [-edaxzflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup] [-Z psetidlist]

I don't have root acces to this machine, but is the ps source code somewhere that I could compile it into my home directory? I haven't been able to find the source code anywhere with google, but that would make sense since ps comes with every system.
Gold Fish

Last edited by goldfish; 02-14-2008 at 05:37 PM..
# 4  
Old 02-14-2008
Quote:
Originally Posted by goldfish
I've tried this previously. I get "ps: illegal option -- o". My man page says that "-o" is XPG4 Only. Several other options are labeled XPG4 only and are unusable as well.

It seems really odd to me that -o wouldn't work, because as far as I can tell, this makes the availabilty of columns like etime useless, because it doesn't come in any of the pre established lists like -l or -f, so I have no way of displaying it.

To give you a better idea of what I do and don't have available here is my usage:
Code:
usage: ps [-edaxzflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup] [-Z psetidlist]

I don't have root acces to this machine, but is the ps source code somewhere that I could compile it into my home directory? I haven't been able to find the source code anywhere with google, but that would make sense since ps comes with every system.
Gold Fish
If your manual page mentions XPG4 options, it probably also mentions the full path to the XPG4 version of "ps".

If not, do a search on your disk to see if there are another other "ps" commands available.

find / -name ps
# 5  
Old 02-14-2008
There was no mention of a location in the man file (I read though all of it) or how to run it. I am unsure to what XPG4 is but my best guess is that it is some sort of compiler time option, which is of no help to me.

The find command yielded only one other ps command, which sadly reacts the same way as the old one and has the same options.

That was a good thing to try though.

Gold Fish
# 6  
Old 02-14-2008
try to find out the width of each column. They should have a fixed width.

You are interested in column 2,5,7 and 13

Suppose:

width column 1: 9 chars
width column 2: 6 chars
width column 3+4: 15 chars
width column 5: 10 chars
width column 6: 8 chars
width column 7: 6 chars
width column 8-12: 45 chars
width column 13: 20 chars

ps -u<user> | sed -e "s/.\{9\}\(.\{6\}\).\{15\}\(.\{10\}\).\{8\}\(.\{6\}\).\{45\}\(.\{20\}\)/\4 \1 \2 \3/"
# 7  
Old 02-14-2008
Awesome! You've given me a great start! I've been tweeking it here and there to see what I could come up with and I have:

Code:
echo "  PID     STIME   TIME  COMMAND" && ps -u user023 -xf|sed -n 's/.\{9\}\(.\{6\}\).\{9\}\(.\{9\}\).\{9\}\(.\{5\}\).\{66\}saswork\(.*\)$/\1 \2 \3 \4/p'

So for the last argument I move 66 characters forward until I have the word saswork (this works as a filter) and then grab the remaining characters. I also added a 'p' and a '-n', so that I would only get the lines of interest. In my expirements, the 'p' kept having the sed line show up as well, because "saswork" is located in that line too. Luckily saswork just happens to be 67 characters into the process name and not 66 characters, so there is a difference.

Thanks for all your help!
Gold Fish
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Filtering records of a csv file based on a value of a column

Hi, I tried filtering the records in a csv file using "awk" command listed below. awk -F"~" '$4 ~ /Active/{print }' inputfile > outputfile The output always has all the entries. The same command worked for different users from one of the forum links. content of file I was... (3 Replies)
Discussion started by: sunilmudikonda
3 Replies

2. UNIX for Beginners Questions & Answers

Filtering based on column values

Hi there, I am trying to filter a big file with several columns using values on a column with values like (AC=5;AN=10;SF=341,377,517,643,662;VRT=1). I wont to filter the data based on SF= values that are (bigger than 400) ... (25 Replies)
Discussion started by: daashti
25 Replies

3. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

4. UNIX for Dummies Questions & Answers

Filtering data -extracting specific lines

I have a table to data which one of the columns include string of text from within that, I am searching to include few lines but not others for example I want to to include some combination of word address such as (address.| address? |the address | your address) but not (ip address | email... (17 Replies)
Discussion started by: A-V
17 Replies

5. Shell Programming and Scripting

Filtering lines for column elements based on corresponding counts in another column

Hi, I have a file like this ACC 2 2 21 aaa AC 443 3 22 aaa GCT 76 1 33 xxx TCG 34 2 33 aaa ACGT 33 1 22 ggg TTC 99 3 44 wee CCA 33 2 33 ggg AAC 1 3 55 ddd TTG 10 1 22 ddd TTGC 98 3 22 ddd GCT 23 1 21 sds GTC 23 4 32 sds ACGT 32 2 33 vvv CGT 11 2 33 eee CCC 87 2 44... (1 Reply)
Discussion started by: polsum
1 Replies

6. Shell Programming and Scripting

Filtering Multiple variables from a single column

Hi, I am currently filtering a file, "BUILD_TIMES", that has multiple column of information in it. An example of the data is as follows; Fri Nov 5 15:31:33 2010 00:28:17 R7_BCGNOFJ_70.68 Fri Nov 5 20:57:41 2010 00:07:21 R7_ADJCEL_80.6 Wed Nov 10 17:33:21 2010 00:01:13 R7_BCTTEST3_80.1X... (7 Replies)
Discussion started by: crunchie
7 Replies

7. Shell Programming and Scripting

filtering column #7

Hi, I want to filter a file that contains 11 columns. Basically I want to filter based on column 7. The file that I have looks like this: 1 3 4 AB 45 run PPPPPPPOOOOOOOLLLLLLLL... 5 9 ui So for column 7 i want to delete lines that have a . OR more than 2 . So if I were to... (3 Replies)
Discussion started by: phil_heath
3 Replies

8. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

9. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

10. UNIX for Dummies Questions & Answers

Filtering records of a file based on a value of a column

Hi all, I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g. COL1 COL2 COL3 ............... COL47 1 XX 45 ... (4 Replies)
Discussion started by: risk_sly
4 Replies
Login or Register to Ask a Question