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
# 1  
Old 08-06-2009
awk script giving unstable results

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

Last edited by Anteus; 08-06-2009 at 08:16 AM..
# 2  
Old 08-06-2009
print "count" just print the literal string "count".
which shows on the second test
# 3  
Old 08-06-2009
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?
# 4  
Old 08-06-2009
Try the following command :
Code:
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).
Code:
psg ServTest | grep -v "grep" | grep -v "vi" | awk '{cmd_name="ServTest"; print}'

Jean-Pierre.
# 5  
Old 08-07-2009
Quote:
Originally Posted by aigles
Try the following command :
Code:
psg ServTest' | awk '1 ! /vi|awk/ && $8 == "ServTest" { print "ServTest Present" }'

Output
Code:
syntax error at line 2 : `'' unmatched

Can you show us the output of the psg command (the two cases ServTest running and not running).
Code:
psg ServTest | grep -v "grep" | grep -v "vi" | awk '{cmd_name="ServTest"; print}'

Jean-Pierre.

When ServTest running
Output
Code:
 
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
Code:
 
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
# 6  
Old 08-07-2009
Try:
Code:
psg ServTest' | awk '! /vi|awk/ && $8 == "ServTest" { print "ServTest Present" }'


Last edited by danmero; 08-07-2009 at 03:40 AM..
# 7  
Old 08-07-2009
Quote:
Originally Posted by danmero
Try:
Code:
psg ServTest' | awk '! /vi|awk/ && $8 == "ServTest" { print "ServTest Present" }'

Code:
 
syntax error at line 2 : `'' unmatched


Last edited by Anteus; 08-07-2009 at 03:19 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #973
Difficulty: Easy
GCC is the default compiler for the Linux kernel source.
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