What is wrong with my awk command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What is wrong with my awk command?
# 1  
Old 09-29-2016
What is wrong with my awk command?

The below code usually works when the value for the COLUMN variable is numerical. but im in a situation where the number of fields in a file is not definitive. it changes.
but what is static is that the value i want to retrieve from the log is 3 fields from the last field. which is what i attempted to grab with the '(NF-3)'

data:

Code:
costingi.PrimaryCost query {"costType": 1, "season": 1, "year": 1} 1 500 500 500 n/a 1
planningservice.targets query {"ccId": 1, "inDCDate": {"$ne": 1}, "season": 1, "year": 1} 1 160 160 160 n/a 1

code:
Code:
WARN=170
CRIT=200
COLUMN='(NF-3)'

awk -F" " '/query/ && !/nothing/ {if($'$COLUMN' = '$WARN' || $'$COLUMN' = '$CRIT') { print ; w++ } else if($'$COLUMN' = '$CRIT') { print ; c++ } else {o++}} END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }'

However, when i run the above command, it simply outputs everything in the file, instead of just the specific lines that have a value greater than the
# 2  
Old 09-29-2016
Hello SkySmart,

Variable defining in awkdoesn't work like in SHELL, could you please change you code to following and let me know if this helps.
Code:
awk -vwarn="$WARN" -vcrit="$CRIT" -vcolumn="$COLUMN" -F" " '/query/ && !/nothing/ {if($column = warn || $column = crit) { print ; w++ } else if($column = crit) { print ; c++ } else {o++}} END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }'

Also as you haven't provided any sample Input_file or expected output so I haven't tested it at all.

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-29-2016 at 11:48 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-29-2016
Quote:
Originally Posted by SkySmart
The below code usually works when the value for the COLUMN variable is numerical. but im in a situation where the number of fields in a file is not definitive. it changes.
but what is static is that the value i want to retrieve from the log is 3 fields from the last field. which is what i attempted to grab with the '(NF-3)'

data:

Code:
costingi.PrimaryCost query {"costType": 1, "season": 1, "year": 1} 1 500 500 500 n/a 1
planningservice.targets query {"ccId": 1, "inDCDate": {"$ne": 1}, "season": 1, "year": 1} 1 160 160 160 n/a 1

code:
Code:
WARN=170
CRIT=200
COLUMN='(NF-3)'

awk -F" " '/query/ && !/nothing/ {if($'$COLUMN' = '$WARN' || $'$COLUMN' = '$CRIT') { print ; w++ } else if($'$COLUMN' = '$CRIT') { print ; c++ } else {o++}} END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }'

However, when i run the above command, it simply outputs everything in the file, instead of just the specific lines that have a value greater than the
you cannot do it like this - you cannot expect the COLUMN be evaluated at run time.
consider the following - not aligned/tested:
Code:
WARN=170
CRIT=200
COLUMN=3
awk -v col="${COLUMN}" -v warn="${WARN}" -v crit="${CRIT}" '
/query/ && !/nothing/ {
   if($(NF-col) == warn ||  $(NF-col) == crit) 
    {print ; w++ } 
  else if($(NF-col) == crit) 
   {  print ; c++ } 
  else o++
}
END { printf("%d:OK %d:WARNING %d:CRITICAL\n",  o, w, c) }'

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 09-29-2016
unfortuntely, none of the suggested commands work.

i need to be able to specify the column to look into. most columns are counted from left to right. however, due to spaces/tabs or formatting differences in files, one may want to grab a column based on the count of right to left.

The solution i need should be able to take a regular numerical value AND also a value like "(NF-whateverNUmber)" which means, count from right to left.

The full data file looks like this:

Code:
costingintegration.ExceptionLogEntry     query         {"_id": 1, "retryFlag": {"$ne": 1}}            1     3778     3778      3778    n/a          3778
costingintegration.negotiatedProductCosts   query         {"$and": [{"season": 1}, {"year": 1}]}            9      138      735       410    n/a          3696
costingintegration.negotiatedProductCosts   query         {"brand": 1, "season": 1, "year": 1}              6      109     2447       534    n/a          3208
costingintegration.$cmd            count         {"costType": 1, "season": 1, "year": 1}           2     1151     1536      1343    n/a          2687
costingintegration.PrimaryCost        query         {"season": 1, "year": 1}                    2      271     1905      1088    n/a          2176
costingintegration.negotiatedProductCosts   getmore       {"$and": [{"season": 1}, {"year": 1}]}           11      104      353       148    n/a          1630
masteritem.$cmd      findandmodify    {"_id": 1}                 1     1451     1451      1451    n/a          1451
costingintegration.$cmd            count         {"season": 1, "year": 1}                    1     1012     1012      1012    n/a          1012
costingintegration.PrimaryCost        query         {"costType": 1, "season": 1, "year": 1}           1      500      500       500    n/a          500
costingintegration.PrimaryCost        getmore       {"bomCCNumber": 1, "season": 1, "year": 1}                      1      252      252       252    n/a          252
planningservice.targets            query         {"ccId": 1, "inDCDate": {"$ne": 1}, "season": 1, "year": 1}           1      160      160       160    n/a          160
costingintegration.$cmd            count         {"brand": 1, "season": 1, "year": 1}              1      136      136       136    n/a          136

in other words, based on the log file, sometimes, i may need the value of COLUMN to be "4" meaning grab column $4. Or if the format of the log is unusual, i may need to say, grab the 3rd column from the right. so essentially, i need to be able to specify:

Code:
COLUMN=4
or
COLUMN='(NF-3)'


Last edited by SkySmart; 09-29-2016 at 12:06 PM..
# 5  
Old 09-29-2016
try this and pass -v col="${COLUMN}" as you see fit where COLUMN is either positive or negative (either 4 or -3 in your example):
Code:
/query/ && !/nothing/ {
  cn=(col>0)?col:NF+col
  if($cn == warn ||  $cn == crit)      
    {print ; w++ }    
  else if($cn == crit)     
           {  print ; c++ }    
          else o++ }


Last edited by vgersh99; 09-29-2016 at 01:35 PM..
# 6  
Old 09-29-2016
Shouldn't that be NF+col if col is negative?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-29-2016
Quote:
Originally Posted by RudiC
Shouldn't that be NF+col if col is negative?
good catch, RudiC - fixed.
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What's wrong with my awk

file1: maximum_delay time: 102.794 ms maximum_delay time: 92.977 ms maximum_delay time: 98.895 ms maximum_delay time: 96.891 ms maximum_delay time: 86.966 ms maximum_delay time: 95.91 ms maximum_delay time: 98.921 ms maximum_delay time: 89.881 ms maximum_delay time: 92.931 ms... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

2. Shell Programming and Scripting

What's wrong with this awk?

I have a file and if I'm doing following action on that file it's coming up correctly awk -F"," '/DATA_TYPE/ { cnt += $3 } END { print " DATA_TYPE count=" cnt}' inter DATA_TYPE count=19593131 now if I'm changing same AWK, so that now it can accept variable, then it's somehow not working.... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

3. Shell Programming and Scripting

what's wrong with my awk

echo "abc:bcd" |awk '{split($0,b,":");print "b" is good }' I want to "bcd" is good , anything is wrong with my, please don't change my thought with split, I just want to know what's wrong :o (1 Reply)
Discussion started by: yanglei_fage
1 Replies

4. Shell Programming and Scripting

What's wrong with this awk?

ZSCORE=$(awk "BEGIN {if($STDEVIATE>0) {print ZSCORER=$(awk "BEGIN{print (${ALL} - ${AVERAGE}) / ${STDEVIATE}}")}else print "0"}") awk: fatal: division by zero attempted awk: BEGIN {if(0==0) {print ZSCORER=}else print 0} awk: ^ syntax error ALL=9 STDEVIATE=0... (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

What's wrong with the following command?

Hi all, I'm trying to run the following command to get all files in one directory to another with the files' timestamps preserved, cp -p /logs/dvgbiau/batch/* /logs/dvgbiau/tmp_batch Note that ./batch and ./tmp_batch are two sub-directories under /logs/dvgbiau. The error was,... (1 Reply)
Discussion started by: isaacniu
1 Replies

6. Shell Programming and Scripting

awk... what is wrong?

Hi there, what is wrong with this? grep ">>" alarms_temp | awk '{print substr($5, 2), $7}' | read var1 var2 echo "$var1" echo "$var2" Both variable are empty, but if i run: grep ">>" alarms_temp | awk '{print substr($5, 2), $7}' I have: 0 9 as a result. (2 Replies)
Discussion started by: marimovo
2 Replies

7. Emergency UNIX and Linux Support

getting wrong output with AWK command!!!

i have a file which gets appended with 9 records daily and the file keeps growing from then...i use to store the previous day files count in a variable called oldfilecount and current files count as newfilecount.my requirement is that i need to start processing only the new records from the... (3 Replies)
Discussion started by: ganesh_248
3 Replies

8. UNIX for Dummies Questions & Answers

what is wrong with this command?

Hello, I try to using the below command to find out all the datafiles under "sja" direcotory. $ xargs -i find {} -type f -ls < sja /bin/ksh: sja: cannot open so can you tell me what is wrong? Thanks Jerry (3 Replies)
Discussion started by: GreatJerry
3 Replies

9. UNIX for Dummies Questions & Answers

AWK command giving wrong input

Hi all, I have a problem with qwk command. i have to check process status and for that i am using command prstat -mvL 1 1 and it gives me the entire output but when i use this command with awk like this: prstat -mvL 1 1 | awk -F" " '{print $1,$15}' to get first and 15th arguments. ... (3 Replies)
Discussion started by: usha rao
3 Replies

10. Shell Programming and Scripting

Is anything wrong with this command

Hi All, can anyone tell me what is wrong with this command. tail -f /opt/olr-logs/PaymentGateway.log | grep "DEBUG - Start! AkhtarPaymentGateway - generateChecksum" | awk '{print $13}' | sed 's/,//g'>> abc But I found nothing in the file abc Please do help me.or Provide me some... (8 Replies)
Discussion started by: akhtar.bhat
8 Replies
Login or Register to Ask a Question