Reg Exp in awk 
Author Message
 Reg Exp in awk

I am trying to tack the process ID's back to 1 in a shell script. I have
cut and pasted the output from ps -ef into one file with only four
fields. THe fields are tab separated with some spaces before the value:
   root^I   2345^I   1^I   /etc/process

The current PID is in variable id. I want to use awk to check the second
column, but I can not work out the regular expression. THe original
/'$id'/ captured all occurances of the id in field two, 123 as well as
the desired 2.

I have tried various combinations without success. THe lastest attempt
was /"\t" *'$id'"\t"/ which I thought should work: the leading tab, any
number of spaces, the id, followed by a tab.

Any advice?

Thanks,
Jeffrey West

-----




Wed, 28 Mar 2001 03:00:00 GMT  
 Reg Exp in awk

Quote:

>I am trying to tack the process ID's back to 1 in a shell script. I have
>cut and pasted the output from ps -ef into one file with only four
>fields. THe fields are tab separated with some spaces before the value:
>   root^I   2345^I   1^I   /etc/process

>The current PID is in variable id. I want to use awk to check the second
>column, but I can not work out the regular expression. THe original
>/'$id'/ captured all occurances of the id in field two, 123 as well as
>the desired 2.

>I have tried various combinations without success. THe lastest attempt
>was /"\t" *'$id'"\t"/ which I thought should work: the leading tab, any
>number of spaces, the id, followed by a tab.

It is unclear what you're trying to do, but if you want to pipe
something into awk that prints the line if the second field is equal
to your shell variable $id, this will do that:

... | awk '$2==a {print $0}' a="$id"

actually yhe {print $0} isn't needed, I just put it there so
you could have something to modify as needed.

... | awk '$2==a' a="$id"

would work just as well.

I hope this helps, as I said, it isn't clear what you want to do.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Wed, 28 Mar 2001 03:00:00 GMT  
 Reg Exp in awk

Quote:

>I am trying to tack the process ID's back to 1 in a shell script. I have
>cut and pasted the output from ps -ef into one file with only four
>fields. THe fields are tab separated with some spaces before the value:
>   root^I   2345^I   1^I   /etc/process

>The current PID is in variable id. I want to use awk to check the second
>column, but I can not work out the regular expression. THe original
>/'$id'/ captured all occurances of the id in field two, 123 as well as
>the desired 2.

>I have tried various combinations without success. THe lastest attempt
>was /"\t" *'$id'"\t"/ which I thought should work: the leading tab, any
>number of spaces, the id, followed by a tab.

>Any advice?

>Thanks,
>Jeffrey West

Jeffrey --

If you want to walk the process tree back up to the
source process ( Pid 1, init ) the following might
get you started.  It is the heart of a program called
scud ( written in early 1991 ;-) that we use to
completely zap a user or a pid and all it's kiddies.

The only regex I needed was used to find the process
name in the ps -ef output and to qualify the input
( iow, to skip the ps -ef header ).  

If I understand your input, all you really need to
do to the following script is to identify your field
numbers in F_PID and F_PPID.

You probably want to set F_Proc to 4 and eliminate
the match () on TimeRegEx -- your proc name is always
4.

The general strategy is to slurp the process parents
into an array ( Dads [Pid] ) and do the process names
as well for good measure ( Proc [Pid] ).

When all the proc lines have been read, walk the tree
from bottom ( requested Pid ) to top ( stopping before
Pid 1 ).

The output of pstree is:  Pid, Dad, Proc Name

HTH.

-- kjh

Example ( see below, file foo is provided for testing )  

$ pstree 24461 foo

24461  24460  ps -ef
24460  24459  /bin/sh
24459  24407  sh -c /bin/sh
24407  24406  term netcom
24406  17835  ksh
17835  21808  ksh
21808      1  -sh

################# cut here ###############
save as pstree and chmod 755
################# cut here ###############
#!/bin/sh

# usage:  pstree PID File_Containing_ps_-ef_Output
# or
#         ps -ef | pstree PID

Pid=$1     # braindead sh script to test gawk script
shift

gawk '
BEGIN {

   WhatPid = "'"$Pid"'"   # this is command line parm 1 expanded by /bin/sh

   # Jeffrey -- change these Field definitions to match your input

   F_PID  = 2      # Process Id Field
   F_PPID = 3      # Parent Process Id Field
   F_Proc = 8      # Process Name is either Field 8 or 9 ( depends on date )

   TimeRegEx = " [0-9]+:[0-9][0-9] "  # Needed to find process name

   Proc [0] = ""
   Dads [0] = ""

Quote:
}

{
   if (( NF >= F_Proc ) && ( match ( $(F_PID), /[0-9]+/ )))
   {
      Pid  = $(F_PID)
      PPid = $(F_PPID)

      Dads [Pid] = PPid

      # SysVr3 uses MMM DD for 'old' process start dates and HH:MM for 'new'
      # the space between MMM and DD makes this a necessary evil because the
      # process name begins at either field 8 or field 9 :-(

      if ( match ( $0, TimeRegEx ) > 0 )
         Proc [Pid] = substr ( $0, RSTART+RLENGTH )
      else
         Proc [Pid] = "undetermined"
   }

Quote:
}

END {

   Dad = WalkPIDTree( WhatPid )

Quote:
}

function WalkPIDTree( Pid,  Dad )
{

   if ( ! ( Pid in Dads ))
   {
      printf ( "Pid %s not found\n", Pid )
      return
   }

   while (( Pid in Dads ) && ( Pid+0 > 1 ))
   {
      printf ( "%5d  %5d  %s\n", Pid, Dads [Pid], Proc [Pid] )
      Pid = Dads [ Pid ]
   }

   return

Quote:
}' $*

exit 0

################# cut here ###############
Here is a ps -ef output from my home box
( saved as foo in the example above )
################# cut here ###############
     UID   PID  PPID  C    STIME TTY      TIME COMMAND
    root     0     0  0  Aug  3  ?        0:00 sched
    root     1     0  0  Aug  3  ?       93:59 /etc/init
    root     2     0  0  Aug  3  ?        0:07 vhand
    root     3     0  0  Aug  3  ?       54:19 bdflush
    root   108     1  0  Aug  3  console  0:04 -sh
    root   110     1  0  Aug  3  vt01     0:00 /etc/getty /dev/vt01 vt01
    root   111     1  0  Aug  3  vt02     0:00 /etc/getty /dev/vt02 vt02
    root    88     1  0  Aug  3  ?       10:17 /etc/cron
    root    92     1  0  Aug  3  ?        9:01 /usr/lib/lpsched
    root   112     1  0  Aug  3  vt03     0:00 /etc/getty /dev/vt03 vt03
    root   113     1  0  Aug  3  vt04     0:00 /etc/getty /dev/vt04 vt04
    root   114     1  0  Aug  3  vt05     0:00 /etc/getty /dev/vt05 vt05
    root   115     1  0  Aug  3  vt06     0:00 /etc/getty /dev/vt06 vt06
    root   116     1  0  Aug  3  vt07     0:00 /etc/getty /dev/vt07 vt07
    root 21808     1  0  Feb 20  tty01    0:01 -sh
    root 17835 21808  0  Oct  3  tty01   2646:22 ksh
    root 24406 17835  0 19:02:27 tty01    0:00 ksh
    root 24407 24406  0 19:02:27 tty01    0:13 term netcom
    root 24410 24407  0 19:02:31 tty01    0:00 ntermrdr 5 6 1 0
    root 24461 24460 11 20:33:28 tty01    0:00 ps -ef
    root 19628   108  0  Jul 21  console  0:01 ksh
    root 24459 24407  0 20:33:23 tty01    0:00 sh -c /bin/sh
    root 24460 24459  0 20:33:23 tty01    0:00 /bin/sh
################# cut here ###############
--
------------------------------------------------------------

1111 Seacoast Dr.  Unit 41   |  home:   (619) 423-4451     |
Imperial Beach, CA   91932   |                             |



Thu, 29 Mar 2001 03:00:00 GMT  
 Reg Exp in awk
[This followup was posted to comp.unix.questions and a copy was sent to
the cited author.]


says...

Quote:


> >I am trying to tack the process ID's back to 1 in a shell script. I have
> >cut and pasted the output from ps -ef into one file with only four
> >fields. THe fields are tab separated with some spaces before the value:
> >   root^I   2345^I   1^I   /etc/process

> >The current PID is in variable id. I want to use awk to check the second
> >column, but I can not work out the regular expression. THe original
> >/'$id'/ captured all occurances of the id in field two, 123 as well as
> >the desired 2.

> >I have tried various combinations without success. THe lastest attempt
> >was /"\t" *'$id'"\t"/ which I thought should work: the leading tab, any
> >number of spaces, the id, followed by a tab.

> It is unclear what you're trying to do, but if you want to pipe
> something into awk that prints the line if the second field is equal
> to your shell variable $id, this will do that:

> ... | awk '$2==a {print $0}' a="$id"

> actually yhe {print $0} isn't needed, I just put it there so
> you could have something to modify as needed.

> ... | awk '$2==a' a="$id"

> would work just as well.

> I hope this helps, as I said, it isn't clear what you want to do.

> Chuck Demas
> Needham, Mass.

> --
>   Eat Healthy    |   _ _   | Nothing would be done at all,

>   Die Anyway     |    v    | That no one could find fault with it.


My problem is in column 2. The data has a variable number of spaces. When
I set id=' '1964 for example, then the code works. However, when the
value is simply 1964, awk does not find the given line in the input file.
I am pretty sure it's because of the spaces.

So my question is this: I can not figure out the regualr expression to
allow for the spaces. I have tried various combinations, and I am not
having an success.

THe other problem I ran into was when the id was 3, for example, all
instances of 3 were being returned including 3, 310, 123, etc.

I appreciate all the help I have received here.



Thu, 29 Mar 2001 03:00:00 GMT  
 Reg Exp in awk
: I am trying to tack the process ID's back to 1 in a shell script. I have
: cut and pasted the output from ps -ef into one file with only four
: fields. THe fields are tab separated with some spaces before the value:
:    root^I   2345^I   1^I   /etc/process

: The current PID is in variable id. I want to use awk to check the second
: column, but I can not work out the regular expression. THe original
: /'$id'/ captured all occurances of the id in field two, 123 as well as
: the desired 2.

: I have tried various combinations without success. THe lastest attempt
: was /"\t" *'$id'"\t"/ which I thought should work: the leading tab, any
: number of spaces, the id, followed by a tab.

: Any advice?

: Thanks,
: Jeffrey West

: -----


BTW,  If you have an XPG4 ps,  as in HP-UX 10.x,  you can
get a hierarchical listing using the -H option.   XPG4 mode
is accessed by setting the UNIX95 var:

UNIX95=1 ps -H

or

export UNIX95=1
ps -H

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Thu, 29 Mar 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Reg Exp Search in Asm?

2. Is // a reg exp?

3. reg exp help needed

4. Why no reg exp in Forth?

5. variables in reg exp repetition

6. Reg-exp library?

7. Newbie Generic Reg Exp Pattern Matching Question

8. Newbie: searching an English dictionary for a reg exp

9. Reg Exp. Problem

10. Reg Exp: Need advice concerning "greediness"

11. Expect reg-exp trouble

12. IA - Satellite Commun Project, sw engr exp., satellite com exp, ADA

 

 
Powered by phpBB® Forum Software