Sco Unix Grep Specific Number (ID)

173 Views Asked by At

I have a programme I am writing for work.

it connects onto our Sco Unix server and runs a command, which most of the time works fine

String command = "ps -eo ruser,pid,ppid,stime,etime,tty,args | sort -k4 | grep /PGProcid="+ProcID+"\\ | grep -v grep"; 

Which when output can look like the following for example

ps -eo ruser,pid,ppid,stime,etime,tty,args | sort -k4 | grep /PGProcid=1\ | grep -v grep

However if I try and do this for a single number (normally 1 but not restricted to it) I get no results, even though I know the results exist.

for example if I have the following results on the server

# ps -ef | grep /PGProcid=1
 name 29175 29174  0 02:55:57  ttyp15    00:00:00 /xxx/xxx/xxx/prog6 /PGProcid=14
 person2 28201 28199  0 01:15:27  ttyp13    00:00:00 /xxx/xxx/xxx/prog1 /PGProcid=1

Then if I do the following

# ps -ef | grep /PGProcid=1\

I get no results but I know there is results for 1, the above will work if i use double digits like 14 will bring back the results.

I basically need to be able to grep for the /PGProcid= to get the PID and PPID numbers. This only seems to not work where there's 1 & 10,11,12 etc or 2 & 20,21,22 so on.

I have tried Egrep, and using $'s but it always seem to skip the single digit numbers!

EDIT: Here's what i have tried on this server

  # echo $SHELL
  /bin/sh
  ps -ef | grep PGProcid=2
  amanda 23602 25207  0 09:22:58       ?    00:00:06 /xxxxxx /PGProcid=2
  amanda 25207 25203  0   Feb-28       ?    00:00:01 /xxxxxx /PGProcid=2
  root 26389 26034  0 05:15:22   ttyp6    00:00:00 grep PGProcid=2
  amanda 26042 23602  0 04:46:16       ?    00:00:04 /xxxxxx /PGProcid=2

so 2 is active currently on their server however the below give no results

  # ps -ef | grep /PGProcid=2$
  # ps -ef | grep /PGProcid=2\$
  # ps -ef | grep "/PGProcid=2$"

The below gives results but also picks up anything with a 2 in it so 22 etc where im only after 2

   # ps -ef | grep '/PGProcid=2$'

Below gives an error "No such file or directory"

   # ps -ef | grep `/PGProcid=2$`
1

There are 1 best solutions below

10
On BEST ANSWER

Your shell will try to expand $ with an environment variable. You have to protect it $ with either a \:

grep /PGProcid=1\$

or "":

grep "/PGProcid=1$"

Edit: To be more precise, you should use the \> to match the empty string at the end of a word. And as both \ and > are interpreted by the shell, you should protect them as well:

grep /PGProcid=1\\\>

or

grep "/PGProcid=1\>"

If you want to have a "word match" (which it seems to me), you can also try the -w option:

grep -w /PGProcid=1