[comp.unix.aix] Easy

moore@emily.uvm.edu (Bryan Moore) (05/09/91)

I am trying to write a shell script (AIX 3.1 ksh) that does the following,
takes the output from an (awk) command, say

STRING1
STRING2
STRING3

and use each of those strings as a parameter to the fgrep of
a system call. 

I want to do a 'ps | fgrep $1' where $1 is each of the above
strings, but obviously the above is incorrect. 

There must be an easy way to do this. I hope my explanation
is understandable.

If I should be posting this to another group, let me know ( I'm
sure someone will!).

Thanks!


BRYAN R. MOORE          	    |   "Last night I had that same old dream
EMAIL: moore@uvm-gen.uvm.edu	    |    it rocked me in my sleep, it gave me
USMAIL: 12 Waybury Rd. Colchester,  |    the impression the sandman plays for 
  Vermont 05446                     |    keeps..."   Larry Norman

scott@prism.gatech.EDU (Scott Holt) (05/10/91)

In article <1991May9.151138.21246@uvm.edu> moore@emily.uvm.edu (Bryan Moore) writes:
>I am trying to write a shell script (AIX 3.1 ksh) that does the following,
>takes the output from an (awk) command, say
>
>STRING1
>STRING2
>STRING3
>
>and use each of those strings as a parameter to the fgrep of
>a system call. 
>
>I want to do a 'ps | fgrep $1' where $1 is each of the above
>strings, but obviously the above is incorrect. 
>
>There must be an easy way to do this. I hope my explanation
>is understandable.

sounds like you want something to the effect of:

PARAMETERS=`awk ...`                       - those are accent characters
if [ "$PARAMETERS" ]
then
   for i in $PARAMETERS
   do
      ps | fgrep $i
   done
else
   echo "Awk found nothing"
fi

the first part is called a command substitution - the command in the
accent characters is executed and the variable (PARAMETERS in this case) is
set to its standard output. The if statement is a check just to make sure
awk found something; it makes sure PARAMETERS is not the null string.

>
>If I should be posting this to another group, let me know ( I'm
>sure someone will!).

yup - I think this is probably a better question for comp.unix.questions or
comp.unix.shell. 

If you really want to dive into this kind of stuff, I would check out
the following books:

   The Korn Shell Command and Programming Language - Bolsky and Korn
or UNIX Shell Porgramming - Revised Edition - Kochan and Wood

- Scott
-- 
This is my signature. There are many like it, but this one is mine.
Scott Holt                 		Internet: scott@prism.gatech.edu
Georgia Tech 				UUCP: ..!gatech!prism!scott
Office of Information Technology, Technical Services

woan@exeter.austin.ibm.com (Ronald S Woan) (05/10/91)

In article <1991May9.151138.21246@uvm.edu> moore@emily.uvm.edu (Bryan Moore) writes:
>I want to do a 'ps | fgrep $1' where $1 is each of the above
>strings, but obviously the above is incorrect. 

ps | awk whatever | xargs -t -i fgrep {}

might do it...



-- 
+-----All Views Expressed Are My Own And Are Not Necessarily Shared By------+
+------------------------------My Employer----------------------------------+
+ Ronald S. Woan                woan@cactus.org or woan@austin.vnet.ibm.com +
+ other email addresses             Prodigy: XTCR74A Compuserve: 73530,2537 +

richard@locus.com (Richard M. Mathews) (05/11/91)

scott@prism.gatech.EDU (Scott Holt) writes:
>moore@emily.uvm.edu (Bryan Moore) writes:
>>I want to do a 'ps | fgrep $1' where $1 is each of the above
>>strings, but obviously the above is incorrect. 

>sounds like you want something to the effect of:

>PARAMETERS=`awk ...`                       - those are accent characters
>if [ "$PARAMETERS" ]
>then
>   for i in $PARAMETERS
>   do
>      ps | fgrep $i
>   done
>else
>   echo "Awk found nothing"
>fi

But do you really want to run "ps" repeatedly for each string?  And do you
want a line of "ps" output to be printed multiple times if more than one
string appears in that line?  How about:

awk ... > /tmp/foo.$$
ps | fgrep -f /tmp/foo.$$
rm -f /tmp/foo.$$

(or set up a "trap" to do the "rm", so the temp file gets removed even if
the script is killed by a signal).

Richard M. Mathews			 Freedom for Lithuania
richard@locus.com				Laisve!
lcc!richard@seas.ucla.edu
...!{uunet|ucla-se|turnkey}!lcc!richard

leland@cs.columbia.edu (Lee Woodbury) (05/14/91)

In article <1991May9.151138.21246@uvm.edu> moore@emily.uvm.edu (Bryan Moore) writes:
>I am trying to write a shell script (AIX 3.1 ksh) that does the following,
>takes the output from an (awk) command, say
>
>STRING1
>STRING2
>STRING3
>
>and use each of those strings as a parameter to the fgrep of
>a system call. 
>
>I want to do a 'ps | fgrep $1' where $1 is each of the above
>strings, but obviously the above is incorrect. 
>
>There must be an easy way to do this. I hope my explanation
>is understandable.

If you mean that the STRINGs are patterns for which you're searching
the ps output (as opposed to fgrep command-line options you're
assembling), then the following may be helpful.

Unless you have some (hidden) reason to use fgrep, you can get a fast
and elegant solution using the more powerful egrep, without resorting
to temporary files or multiple calls to ps (as suggested by some other
responses to your query), as follows:

	ps | egrep "$(your_awk_script)"

This presumes that your_awk_script outputs newline-separated patterns
(as you show in your example), which will thus adhere to the syntax of
egrep's alternation (OR) pattern.

If your_awk_script outputs space-separated patterns, then you'll have
to filter the output through sed, like so:

	ps | egrep "$(your_awk_script | sed 's/ /|/g')"

In this case, the sed script replaces the spaces between
your_awk_script's output patterns with vertical bars, which are the
same as newlines as far as egrep is concerned.

[ Note: The query specified (AIX 3.1) ksh.  Bourne shell users should
replace the $(command substitution) syntax shown in these examples with
the more familiar `backquoting` mechanism used in both sh and ksh. ]

Note that egrep is generally faster than fgrep anyway, and obviously
so if you have to run it multiple times on the output of ps to get
what you want.

If you must use fgrep (for reasons you haven't given), then you'll have
to save the output of ps to a temporary file and, in a loop bounded by
the number of search patterns output by your_awk_script, fgrep the ps
output once for each of those patterns, as shown in at least one
earlier response.

Hope this helps.

Leland Woodbury
-- 
INTERNET: leland@cs.columbia.edu
  USENET: ...!columbia!cs.columbia.edu!leland
  BITNET: leland%cs.columbia.edu@cuvmb
  USMAIL: Columbia Univ., 457 CS, 500 W. 120 St., NYC 10027-6699