How to extract second field in ps command output?

How to extract second field in ps command output?

Post by Jacek A. Ponars » Tue, 27 Jun 1995 04:00:00



Hi,
How can I extract the second field in the ps command output? (the
process id #)
f.e.
ps -ef | grep gopher

root  1402     1 80   Jun 15 ?        0:43
/usr/local/etc/gopherd/gopherd -u
 nobody -l /var/log/gopher

Can I somehow do it with sed or awk or some shell?  Thanks in advance

------------------------------------------------------------------

Albert Einstein College of Medicine   fax:           (718)430-2488
RIT, Bronx, NY.   http://micro6.trincoll.edu, tel:   (718)430-4211
------------------------------------------------------------------

 
 
 

How to extract second field in ps command output?

Post by Beirne Konars » Wed, 28 Jun 1995 04:00:00



: Hi,
: How can I extract the second field in the ps command output? (the
: process id #)
: f.e.
: ps -ef | grep gopher

: root  1402     1 80   Jun 15 ?        0:43
: /usr/local/etc/gopherd/gopherd -u
:  nobody -l /var/log/gopher

: Can I somehow do it with sed or awk or some shell?  Thanks in advance

You have lots of choices.

Sed:
ps -ef | sed -n 's/^[   ][      ]*[^    ][^     ]*[     ][      ]*\([^  ][^     ]*\).*gopher[d]/\1/p'

A better sed will let you simplify the regular expression.

Awk:

ps -ef | awk '/gopher[d]/ { printf $2 }'

Perl:

ps -ef | perl -n -e 'print ((split)[2]) if (/gopher[d]/);'

Perl is the fastest on my machine.  Perl 5 has a cleaner way to do this.

Shell:

ps -ef |  grep gopher[d] | read f1 f2 rest;print $f2

There are lots of other ways to do this in shell.

Beirne

: ------------------------------------------------------------------

: Albert Einstein College of Medicine   fax:           (718)430-2488
: RIT, Bronx, NY.   http://micro6.trincoll.edu, tel:   (718)430-4211
: ------------------------------------------------------------------

--
Beirne Konarski         | Subscribe to the Unicycling Mailing List

Kent State University   | Unicycling Web Page:
"Untouched by Scandal"        | http://nimitz.mcs.kent.edu/~bkonarsk/

 
 
 

How to extract second field in ps command output?

Post by Bill Marc » Fri, 30 Jun 1995 04:00:00




Quote:>Hi,
>How can I extract the second field in the ps command output? (the
>process id #)
>f.e.
>ps -ef | grep gopher

>root  1402     1 80   Jun 15 ?        0:43
>/usr/local/etc/gopherd/gopherd -u
> nobody -l /var/log/gopher

>Can I somehow do it with sed or awk or some shell?  Thanks in advance

ps -ef | grep gopher | awk '{print $2}'

--

SEX IS NOT *, CENSORSHIP IS!

 
 
 

How to extract second field in ps command output?

Post by Larry Gardn » Sat, 01 Jul 1995 04:00:00



: Hi,
: How can I extract the second field in the ps command output? (the
: process id #)
: f.e.
: ps -ef | grep gopher

: root  1402     1 80   Jun 15 ?        0:43
: /usr/local/etc/gopherd/gopherd -u
:  nobody -l /var/log/gopher

: Can I somehow do it with sed or awk or some shell?  Thanks in advance

################################################################################
#   F U N C T I O N - Check for running process where $1 is the process name.
#
#   CheckForRunningProcess()
#
# Usage: CheckForRunningProcess gopherd
#
################################################################################
CheckForRunningProcess() {
    if [ -n "$(ps -e | sed -n "/ sed /!p" | \
         sed -n "/${1}/s/\([1-9][0-9]*\) .*${1}/\1 ${1}/p")" ] ; then
        return 0
    else
        return 1
    fi

Quote:}

: ------------------------------------------------------------------

: Albert Einstein College of Medicine   fax:           (718)430-2488
: RIT, Bronx, NY.   http://micro6.trincoll.edu, tel:   (718)430-4211
: ------------------------------------------------------------------

Larry
--
        ______    _____________________________________________________________
       ____    /    ____________  Larry Gardner - IT Engineering Consultant __
      ___     /        ________  ISST - CCG  Workstation COE Group         __

    _       __  /  __  /  ___  US Mail: 20 Perimeter Summit Boulevard NE __
   __      /   /  /   /   __           Atlanta, GA  30319-1417          __
  ____   _/  _/  ____/   __  Comments are mine alone and not those of  __
 ______         /      ___  of my employer.                           __
____________  _/ ______________________________________________________

 
 
 

How to extract second field in ps command output?

Post by Chris de Longpr » Wed, 09 Aug 1995 04:00:00


In ksh, do this

        ps -ef | awk '{print $2}'

This parses out just the PID#'s.  You can feed them to a loop --
to kill processes, for instance all tty sessions, do this:

for i in `ps -ef | grep tty | awk '{print $2}'`
do
  kill $i
done

Hope this helps.  --CdL

--
Chris de Longpre (formerly Gesler), Unix System Administrator
Priority Health, Grand Rapids, MI

 
 
 

1. How do I extract a field between two other fields?

given a file like so:
 VG Name                /dev/vg03
   PV Name                /dev/dsk/c0d0s2
VG Name                /dev/vg01
   PV Name                /dev/dsk/c1d0s2
VG Name                /dev/vg00
   PV Name                /dev/dsk/c2d0s2
VG Name                /dev/vg02
   PV Name                /dev/dsk/c3d0s2
VG Name                /dev/vg07
   PV Name                /dev/dsk/c6d0s2
VG Name                /dev/vg05
   PV Name                /dev/dsk/c7d0s2
VG Name                /dev/vg06
   PV Name                /dev/dsk/c8d0s2
VG Name                /dev/vg04
   PV Name                /dev/dsk/c9d0s2

I need to extract the PV field(s) between the VG Name field(not including
the VG Name field) as there could be more than one PV Name field per volume
group.

I am not sure of how to do this, I am assuming that grep,awk, or sed is
involved Would anyone be as so kind as to provide me with a snippet of code
that may produce the output that I want?
Any help would be greatly appreciated!!

2. What does this error mean?

3. LS command question (How do I omit the LINK field from the output of the command)?

4. Can't compile Debian kernel 2.4.16-1

5. Extracting the "Name" field from "finger" output

6. getting lib

7. why is args field in ps output limited to 80 chars ?

8. ANNOUNCE: Desktop Environment

9. How to assign the output of a command to be my field separator in awk

10. ps COMMAND field

11. how to kill a process extracted from ps command?

12. meaning of WCHAN field in ps command

13. Sorting by second field. Quicker command?