Visit Our UNIX and Linux User Community


awk script giving unstable results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script giving unstable results
# 8  
Old 08-07-2009
Code:
psg ServTest | awk '! /vi|awk/ && $8 == "ServTest" { print "ServTest Present" }'

jean-Pierre.
# 9  
Old 08-11-2009
Quote:
Originally Posted by Anteus
Hi all

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

snippet 1
Code:
 
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
Code:
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
Code:
 
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
Why is code snippet 1 and 3 giving different output.?
Can anyone tell me this.

Thanks and Regards.
# 10  
Old 08-11-2009
Please show us the result of the psg command, add a debug print instruction in the awk scripts

snippet 1
Code:
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:
Code:
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.
# 11  
Old 08-14-2009
snippet 1
Code:
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:
Code:
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

Code:
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.

Last edited by Anteus; 08-14-2009 at 03:35 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #990
Difficulty: Medium
UNIX System V is one of the first commercial versions of the Unix operating system and was originally developed by AT&T and first released in 1981.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - saving results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

2. Shell Programming and Scripting

Comm giving unexpected results

Hi I am comparing two files with comm -13 < (sort acc11.txt) < (sort acc12.txt) > output.txt purpose: Get non matching records which are in acc12 but not in acc11... TI am getting WRONG output. Is there any constraints with record length with comm? The above files are the two consective ... (2 Replies)
Discussion started by: vedanta
2 Replies

3. UNIX for Dummies Questions & Answers

Grep not giving expected results

Version: RHEL 5.8 I am doing a grep of the piped output from ps command as shown below. I am grepping for the pattern ora_dbw* . But, in the result set I am seeing strings with ora_dbr* as well like ora_dbrm_SDLM1DAS3 as shown below. Any idea why is this happening ? $ ps -ef | grep... (6 Replies)
Discussion started by: John K
6 Replies

4. Shell Programming and Scripting

Solaris script using awk giving errors - please advise

I'm using solaris 10 Scenario as follows I have a logfile with 2 columns: column 1 = source directory + filename column 2 = destination directory + filename Using cron, my script polls for new files and adds them to the logfile ($ELOG) as described above. Using sed, the distination... (2 Replies)
Discussion started by: davidra
2 Replies

5. Shell Programming and Scripting

awk sum giving incorrect value

cat T|awk -v format=$format '{ SUM += $1} END { printf format,SUM}' the file T has below data usghrt45tf:hrguat:/home/hrguat $ cat T -1363000.00123456789 -95000.00789456123 -986000.0045612378 -594000.0015978 -368939.54159753258415 -310259.0578945612 -133197.37123456789... (4 Replies)
Discussion started by: zulfi123786
4 Replies

6. Shell Programming and Scripting

Sed in vi - \r and \n not giving desired results

I use many different machines at work, each with different versions of o/s's and installed applications. Sed in vi is particularly inconvenient in the sense that sometimes it will accept the "\r" as a carriage return, sometimes not. Same thing with "\n". For instance, if I have a list of hosts... (7 Replies)
Discussion started by: MaindotC
7 Replies

7. Shell Programming and Scripting

awk script to parse results from TWO files

I am trying to parse two files and get data that does not match in one of the columns ( column 3 in my case ) Data for two files are as follows A.txt ===== abc 10 5 0 1 16 xyz 16 1 1 0 18 efg 30 8 0 2 40 ijk 22 2 0 1 25 B.txt ===== abc... (6 Replies)
Discussion started by: roger67
6 Replies

8. HP-UX

find -mtime giving strage results in HP-UX

Hi, I am using HP-UX B.11.23 U ia64 I am trying to retrieve files using -mtime option of find command However I found that -mtime is not giving correct results Following is the output of commands executed on 03-Dec-2009 It can be seen that -mtime +1 should have returned all... (2 Replies)
Discussion started by: Chetanaz
2 Replies

9. Shell Programming and Scripting

egrep not giving desired results

I have written a shell script which looks like below: grep -v ',0,' ./DATA/abc.001 > ./DATA/abc.mid egrep $GREPSEARCH ./DATA/ebc.mid > ./DATA/abc.cut the variable GREPSEARCH has values like the below: ... (3 Replies)
Discussion started by: igandu
3 Replies

10. Shell Programming and Scripting

Script giving wrong results....

hi, I have this script which gives me the result... #! /usr/bin/sh set -x cd /home/managar a=1 while true do if then echo " File log.txt exists in this directory " exit 0 fi echo " File has not arrived yes..." sleep 3 let a=a+1 if then (1 Reply)
Discussion started by: mgirinath
1 Replies

Featured Tech Videos