It is currently Tue, 07 Dec 2021 03:06:14 GMT



 
Author Message
 awk processing in sh script
Hello, can anyone help?

I am trying to write a simple awk script (in a sh script file) to
process a 'ps' listing (under Interix) to 'seek and destroy' a process
beginning with the substring "tt_"

Due to the peculiarity of Interix, I cannot guarantee that a "ps"
listing of processes will report the target process without the
leading path, therefore I am needing to parse each ps name through
"basename" to extract the single part of the pathname I need.

My original code looked something like (I forget the exact syntax)...

  #!/bin/sh
  pids=ps | awk '$4 ~ /^tt_/ { print $1 }'
  kill $pids

However, this won't work if my process name in the ps listing includes
the path (as Interix seems hell-bent on giving me the full path, as
run - i.e. "./tt_xxxx", or "/home/stuff/tt_xxx", etc.

Is there a way of embedding the basename command into the awk command?
(If 'basename' is Interix specific (doubtful), I apologize - all you
need to know is that "basename 'aaa/bbb/ccc' == ccc")
I tried it a few ways, but the script just seems to embed the word
'basename' into the command, or doesn't work. - i.e. along the lines
of:-

  pids=ps | awk '`basename $4` ~ /^tt_/ {print $1}'
  #doesn't work - is there an alternative to achieve what I'm
  #trying to get at?

Or is there in fact a better way of approaching the problem (given
that I can't change the nature of tt_xxx or how it is launched in any
way).

Thanks in advance.

Mark.



 Tue, 25 Dec 2001 03:00:00 GMT   
 awk processing in sh script
On Fri, 9 Jul 1999 u...@company.com wrote:

One solution: convert that to something like

if ($4 ~ /^tt_/ || $4 ~ /\/tt_[^\/]*$/) {print $1}

Note: syntax maybe incorrect. I don't remember awk that well.

Anand

P.S: If you had provided your email address I would have emailed
you instead of bothering the newsgroup.



 Tue, 25 Dec 2001 03:00:00 GMT   
 awk processing in sh script
: I am trying to write a simple awk script (in a sh script file) to
: process a 'ps' listing (under Interix) to 'seek and destroy' a
: process beginning with the substring "tt_"

If you put the following in an executable file named "dieps",
then invoke as "dieps tt_", does it work?

#!/usr/local/bin/perl -w
for(split(/\n/,`ps -o comm,pid`)){
    s!^($ARGV[0]\S*)\s+(\d+)!! && kill 9,$2;

(I don't know why some people dislike perl's syntax... :)

Main question is: does "-o comm,pid" option do what you
wanted in Interix?

--
Scott Lanning: slann...@buphy.bu.edu, http://physics.bu.edu/~slanning
"It showed a lady, with a fur cap on and a fur stole, sitting upright
and holding out to the spectator a huge fur muff into which the whole
of her forearm had vanished!" --From Franz Kafka's Metamorphosis



 Wed, 26 Dec 2001 03:00:00 GMT   
 awk processing in sh script
: #!/usr/local/bin/perl -w
: for(split(/\n/,`ps -o comm,pid`)){
:     s!^($ARGV[0]\S*)\s+(\d+)!! && kill 9,$2;
: }

...also, if your command name never has a digit in it:

kill -9 `ps -o comm,pid|sed 's/tt_[^0-9]*//p;d'`

--
Scott Lanning: slann...@buphy.bu.edu, http://physics.bu.edu/~slanning
"One should not confuse this craving for change and novelty with the
indifference of play which is in its greatest levity at the same time
the most sublime and indeed the only true seriousness." --Georg Hegel



 Wed, 26 Dec 2001 03:00:00 GMT   
 awk processing in sh script

Thankyou all for your prompt replies which I will try as soon as
possible.

Can anyone comment on a solution using 'basename' in awk still, or am
I on to a loser here...? - is there not some way to incorporate an
expression into the awk parameters?

i.e. along the lines of awk '`basename $4 ~ /^tt_/ { print $1 }'

Thanks

Mark.



 Wed, 26 Dec 2001 03:00:00 GMT   
 awk processing in sh script

Here's a small hint:

("basename" $4) | getline progname; close("basename" $4)

will run basename against the fourth field of the current line and stuff
the output into the variable progname, then close the pipe.  This assumes
you have access to the newer version of awk; check your awk man page's
description of getline to see if piping is supported.  (On some systems,
you may need to use nawk or gawk.)

--
Eric Amick
Columbia, MD
eam...@clark.net



 Wed, 26 Dec 2001 03:00:00 GMT   
 awk processing in sh script
["u...@company.com"?]

In article <GOHh3.31$%w4.1...@iad-read.news.verio.net> Eric Amick
<eam...@shell.clark.net> notes that modern awks can handle:

On the other hand, I think it should be noted that all "basename"
does is strip off a leading regular expression matching ".*/" and
an optional trailing RE matching "\..*$" (you can anchor the first
-- i.e., "^.*/" -- but there is no need since .* matches the longest
sequence possible; and you only need to anchor the second with "$"
if you apply it before the first).

Since the task in question is to find pathnames that end in "tt_.*"
as their final component, any suffix that basename might strip is
uninteresting, and the whole thing can be written as a simple RE
directly in awk:

        $4 ~ /.*\/tt_/

which should work even in ancient "awk"s.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc
El Cerrito, CA  Domain: to...@bsdi.com +1 510 234 3167
http://claw.bsdi.com/torek/  (not always up)    I report spam to abuse@.



 Wed, 26 Dec 2001 03:00:00 GMT   
 awk processing in sh script
Thank you all for your help.
It's given me plenty of new avenues of exploration.

Thanks again.
Mark.



 Thu, 27 Dec 2001 03:00:00 GMT   
 awk processing in sh script

Not like you to be so sloppy, Chris.  :-)

Your RE doesn't restrict the match to the last pathname component - it
will match "tt_" at the start of any component except the first.
Also the .* is redundant.

Anand's answer, elsewhere in this thread, was a much better attempt.

--
Geoff Clare                         g...@unisoft.com
UniSoft Limited, London, England.   g...@root.co.uk



 Mon, 31 Dec 2001 03:00:00 GMT   
 awk processing in sh script
[I suggested]

Ack, quite right.  I must have been asleep when I wrote that.
(Actually, I think I was thinking about "writing basename as a sed
script", rather than "matching a similar field in awk".  I also
seem to have forgotten that basename strips a specified trailing
*string*, not an RE.  [Incidentally, what should basename do when
asked to strip a suffix containing a slash, or equalling the entire
string remaining after removing prefix directories?  Our man page
claims it emits a blank line in the latter case, but experimentation
proves otherwise:

        % basename abc bc
        a
        % basename abc abc
        abc
        % basename .def .def
        .def
        % basename a.def .def
        a
        % basename /a/b/c/def b/c.def
        c.def
        % basename /a/b/c.def c.def
        c.def

So at the least, the BSD/OS man page and program disagree slightly.])

In this case, `contains "tt_" followed by anything except "/" and
then ending at the end of $4' would be much more reasonable:

        $4 ~ /tt_[^/]*$/

I actually tested the above, this time. :-)
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc
El Cerrito, CA  Domain: to...@bsdi.com +1 510 234 3167
http://claw.bsdi.com/torek/  (not always up)    I report spam to abuse@.



 Tue, 01 Jan 2002 03:00:00 GMT   
 awk processing in sh script

The on-line Single Unix Spec (v2) says that the prefix directories are
removed before any suffix processing is done.  It also says that the
suffix removal is only done if "the suffix operand is present, is not
identical to the characters remaining in string, and is identical to a
suffix of the characters remaining in string".  I assume that the SUSv2
text is lifted straight from POSIX.2, although I haven't checked.

Looks like your man page is out of date.
--
Geoff Clare                         g...@unisoft.com
UniSoft Limited, London, England.   g...@root.co.uk



 Sat, 05 Jan 2002 03:00:00 GMT   
 
   [ 11 post ] 

Similar Threads

1. awk and sh variables in a shell script

2. awk and sh variables in a shell script

3. Passing a string Varible from AWK to a sh Script

4. sh script using awk, argument issue?

5. sh vars in awk scripts??

6. Execute a sh script under perl and sh: sh script; perl script?

7. sed/awk - generating Makefiles (Re: awk processing of Makefile macros)

8. make sh-script compatible between SUN-sh and bash

9. script or command to find all scripts (ksh, sh, perl, etc) in a workspace

10. Can you get the name of the calling prg/script from sh script


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software