awk script giving unstable results

Hi all

Here I came accross a situation which i am unable to reason out...

snippet 1

 
psg ServTest | grep -v "grep" | grep -v "vi" | awk '{
     pgm_name=$8
     cmd_name="ServTest"
     gsub(/[[:space:]]*/,"",pgm_name)
     if(pgm_name==cmd_name) { print "ServTest Present =" cmd_name}
}'
Output is 
awk
ServTest not running! = ServTest
 anteus 12576 12572  1 05:31:52 pts/t41   0:00 awk {
ServTest not running! = ServTest
     pgm_name=$8
ServTest not running! = ServTest
    cmd_name="ServTest"
ServTest not running! = ServTest
gsub(/[[:spa
ServTest Present =ServTest
 

snippet 2

psg ServTest  | grep -v "grep" | grep -v "vi" | awk '{
     print "Count"
     pgm_name=$8
     cmd_name="ServTest"
     gsub(/[[:space:]]*/,"",pgm_name)
     if(pgm_name==cmd_name) { print "ServTest Running" }
}'
Output is
count
ServTest Running

even if we comment the line print "Count" .the code gives expected output.
snippet 3

 
psg ServTest  | grep -v "grep" | grep -v "vi" | awk '{
     #print "Count"
     pgm_name=$8
     cmd_name="ServTest"
     gsub(/[[:space:]]*/,"",pgm_name)
     if(pgm_name==cmd_name) { print "ServTest Running" }
}'
Output is
ServTest Running

can any one tell me why is this behavior....??

Thanks and Regards

print "count" just print the literal string "count".
which shows on the second test

yeah that is correct but Why is the code snippet 1 giving different output..

the output of snippet 1 and 3 shoud be same rite?

Try the following command :

psg ServTest' | awk '1 ! /vi|awk/ && $8 == "ServTest" { print "ServTest Present" }'

Can you show us the output of the psg command (the two cases ServTest running and not running).

psg ServTest | grep -v "grep" | grep -v "vi" | awk '{cmd_name="ServTest"; print}'

Jean-Pierre.

When ServTest running
Output

 
awk
ServTest not running! = ServTest
 s0lawre  3408  3404  1 00:06:33 pts/t8    0:00 awk {
ServTest not running! = ServTest
     pgm_name=$8
ServTest not running! = ServTest
    cmd_name="ServTest"
ServTest not running! = ServTest
gsub(/[[:spa
ServTest Present =ServTest

When ServTest not Running
Output

 
awk
ServTest not running! = ServTest
 s0lawre  3566  3562  1 00:07:41 pts/t8    0:00 awk {
ServTest not running! = ServTest
     pgm_name=$8
ServTest not running! = ServTest
    cmd_name="ServTest"
ServTest not running! = ServTest
gsub(/[[:spa

I am not looking for an alternative solution for this script.
just want to know why code snippet 1 and 3 gives diff results?

Thanks
Anteus

Try:

psg ServTest' | awk '! /vi|awk/ && $8 == "ServTest" { print "ServTest Present" }'
 
syntax error at line 2 : `'' unmatched
psg ServTest | awk '! /vi|awk/ && $8 == "ServTest" { print "ServTest Present" }'

jean-Pierre.

Why is code snippet 1 and 3 giving different output.?
Can anyone tell me this.

Thanks and Regards.

Please show us the result of the psg command, add a debug print instruction in the awk scripts

snippet 1

psg ServTest | grep -v "grep" | grep -v "vi" | awk '1 {printf "debug -", $0 } {
     pgm_name=$8
     cmd_name="ServTest"
     gsub(/[[:space:]]*/,"",pgm_name)
     if(pgm_name==cmd_name) { print "ServTest Present =" cmd_name}
}'

snippet 2:

psg ServTest  | grep -v "grep" | grep -v "vi" | awk '1 {printf "debug -", $0 } {
     print "Count"
     pgm_name=$8
     cmd_name="ServTest"
     gsub(/[[:space:]]*/,"",pgm_name)
     if(pgm_name==cmd_name) { print "ServTest Running" }
}'

Jean-Pierre.

snippet 1

psg ServTest | grep -v "grep" | grep -v "vi" | awk '1 {printf "debug -", $0 } {
     pgm_name=$8
     cmd_name="ServTest"
     gsub(/[[:space:]]*/,"",pgm_name)
     if(pgm_name==cmd_name) { print "ServTest Present =" cmd_name}
}'

Result debug -ServTest Present =ServTest

snippet 2:

psg ServTest  | grep -v "grep" | grep -v "vi" | awk '1 {printf "debug -", $0 } {
     print "Count"
     pgm_name=$8
     cmd_name="ServTest"
     gsub(/[[:space:]]*/,"",pgm_name)
     if(pgm_name==cmd_name) { print "ServTest Running" }
}'

Result
debug -count
ServTest Running

-----
and this is my code and its output

psg ServTest| grep -v "grep" | grep -v "vi" | awk '{
print "count"
pgm_name=$8
cmd_name="ServTest"
gsub(/[[:space:]]*/,"",pgm_name)
if(pgm_name==cmd_name)
{
  print "ServTest Present =" cmd_name
}
}'

count
count
count
count
count
count
ServTest Present =ServTest

can u please explain me why is count being printed 6 time instead of one.