Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Best way to find word occurances

5 views
Skip to first unread message

Robert Hicks

unread,
Dec 13, 2006, 12:09:31 PM12/13/06
to
I am using exec to do a ps to find out if DEV1, TST1, PRO1 or in the
result. They all 3 should be and I need to know which ones are not.

This will pull out the lines (I am making sure batch is running) the
instance names above:

set result [exec ps -ef | grep batch | grep -v grep]

I need to parse the $result and see which ones are not showing up now.
Would this be a regex or something like a string scan?

Robert

Arnulf Wiedemann

unread,
Dec 13, 2006, 12:53:09 PM12/13/06
to
Robert Hicks wrote:

> set result [exec ps -ef | grep batch | grep -v grep]

I think that will not work. exec does not allow pipes, only arguments.
You will need something like: set result [open "| ps -ef | grep batch | grep
-v grep"].
Arnulf

Robert Hicks

unread,
Dec 13, 2006, 12:57:15 PM12/13/06
to

Oh no, exec does allow pipes. I use that all the time.

Robert

Don Porter

unread,
Dec 13, 2006, 12:57:39 PM12/13/06
to
Arnulf Wiedemann wrote:
> Robert Hicks wrote:
>
>> set result [exec ps -ef | grep batch | grep -v grep]
> I think that will not work. exec does not allow pipes, only arguments.

[exec] allows pipes just fine.

--
| Don Porter Mathematical and Computational Sciences Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

Bruce

unread,
Dec 13, 2006, 1:09:18 PM12/13/06
to

regexp would work fine, string match would also work,
string first is probably easiest.


foreach word {DEV1 TST1 PRO1} {
if {[string first $word $result] < 0} {
puts "$word is NOT in results!"
}
}


Bruce

Ralf Fassel

unread,
Dec 13, 2006, 1:14:51 PM12/13/06
to
* "Robert Hicks" <sig...@gmail.com>

| set result [exec ps -ef | grep batch | grep -v grep]
|
| I need to parse the $result and see which ones are not showing up
| now. Would this be a regex or something like a string scan?

Instead of grep -v grep, you could use awk to print only the program
name (it is always field 8 (?)), and then set an array with the
result:

set result [exec ps -ef | awk {/batch/ && !/grep/ {print $8}}]
foreach prog [split $result "\n"] {
set found($prog)
}

Now check the array for the missing:
set missing [list]
foreach required {DEV1 TST1 PRO1} {
if {![info exists found($required)]} {
lappend missing $required
}
}
puts "MISSING: $missing"

R'

Robert Hicks

unread,
Dec 13, 2006, 1:20:19 PM12/13/06
to
On Dec 13, 1:09 pm, Bruce <bruce-n...@doNOTuseTHIS.com> wrote:
> Robert Hicks wrote:
> > I am using exec to do a ps to find out if DEV1, TST1, PRO1 or in the
> > result. They all 3 should be and I need to know which ones are not.
>
> > This will pull out the lines (I am making sure batch is running) the
> > instance names above:
>
> > set result [exec ps -ef | grep batch | grep -v grep]
>
> > I need to parse the $result and see which ones are not showing up now.
> > Would this be a regex or something like a string scan?
>
> > Robertregexp would work fine, string match would also work,

> string first is probably easiest.
>
> foreach word {DEV1 TST1 PRO1} {
> if {[string first $word $result] < 0} {
> puts "$word is NOT in results!"
> }
>
> }Bruce

Is "< 0" right there?

Robert

Bruce

unread,
Dec 13, 2006, 2:52:33 PM12/13/06
to

Yes, [string first ...] returns the index where the substring is found,
so zero or positive number means the workd was found somewhere, < 0
(specifically -1) means not found.

Bruce

Robert Hicks

unread,
Dec 13, 2006, 3:11:46 PM12/13/06
to

Bruce wrote:
<snip>

> Yes, [string first ...] returns the index where the substring is found,
> so zero or positive number means the workd was found somewhere, < 0
> (specifically -1) means not found.
>
> Bruce

Ah, I did not know that.

Robert

Arnulf Wiedemann

unread,
Dec 14, 2006, 7:32:18 AM12/14/06
to
Robert Hicks wrote:

Yes, you are right. I had a different problem when just cut and pasting.
My fault.
Arnulf

Arnulf Wiedemann

unread,
Dec 14, 2006, 7:34:04 AM12/14/06
to
Don Porter wrote:

> Arnulf Wiedemann wrote:
>> Robert Hicks wrote:
>>
>>> set result [exec ps -ef | grep batch | grep -v grep]
>> I think that will not work. exec does not allow pipes, only arguments.
>
> [exec] allows pipes just fine.
>

You are right. See my other posting in this thread
Arnulf.

0 new messages