awk script modification

can someone help me identify what i'm doing wrong here:

awk -F'|' 'BEGIN{c=0} /./ && /./ {
if ($3 < 2) { print ; c++ } END { print c":OK" }
else if (($3 >= 2) && ($3 < 4)) { print ; c++ } END { print c":WARNING" }
else if ($3 >= 4) { print ; c++ } END { print c":CRITICAL" }
}' /home/jhash/log.txt

error message i get:

awk: cmd. line:2: if ($3 < 2) { print ; c++ } END { print c":OK" }
awk: cmd. line:2:                             ^ syntax error
awk: cmd. line:3: else if (($3 >= 2) && ($3 < 4)) { print ; c++ } END { print c":WARNING" }
awk: cmd. line:3: ^ syntax error
awk: cmd. line:3: else if (($3 >= 2) && ($3 < 4)) { print ; c++ } END { print c":WARNING" }
awk: cmd. line:3:                                                 ^ syntax error
awk: cmd. line:4: else if ($3 >= 4) { print ; c++ } END { print c":CRITICAL" }
awk: cmd. line:4: ^ syntax error
awk: cmd. line:4: else if ($3 >= 4) { print ; c++ } END { print c":CRITICAL" }
awk: cmd. line:4:                                   ^ syntax error

First, you can't have an if statement in an awk condition; if statements can be placed in BEGIN clauses, END clauses, actions, and in function definitions.

Second, you don't associate END clauses with input lines either. All END sections in a awk script are processed in the order in which they were found in the script after all input lines have been read.

Why don't you start over and tell us what this script is supposed to do and show us some sample input and desired output?

1 Like

can someone please help me out with this:

awk -F'|' -v ok=okay 'BEGIN{c=0} /./ && /./ { if ($3 < 0) { print ; ok } else if (($3 >= 2) && ($3 < 4)) { print ; c++ } else if ($3 >= 4) { print ; c++ } } END { print c }' datafile.txt 

datafile.txt:

2015/02/18|19:02:41|4|4|EDI|CRONSCHD|ngf_gep_outb.cron Successfully Completed
2015/02/19|11:32:27|5|4|EDI|TECPTEST|Successfully completed -------------Summary:------------------- Files received to be processed : 1 Files successfully sent to chase: 1 ------------------------------------------

when i run the above awk code, i get response similar to this:

2015/02/18|19:02:41|4|4|EDI|CRONSCHD|ngf_gep_outb.cron Successfully Completed
2015/02/19|11:32:27|5|4|EDI|TECPTEST|Successfully completed -------------Summary:------------------- Files received to be processed : 1 Files successfully sent to chase: 1 ------------------------------------------
1

i need it to output something similar to this:

2015/02/18|19:02:41|4|4|EDI|CRONSCHD|ngf_gep_outb.cron Successfully Completed
2015/02/19|11:32:27|5|4|EDI|TECPTEST|Successfully completed -------------Summary:------------------- Files received to be processed : 1 Files successfully sent to chase: 1 ------------------------------------------
1:OK

so i basically need to add something to the "c++" to classify if the resulting output is OK, WARNING or CRITICAL.

What you are trying to do with OK, WARNING, and CRITICAL isn't at all clear. Showing us input that has nothing to be classified as OK or WARNING makes it difficult to guess what output you're trying to produce. And, using one variable ( c ) to store three different values can't work. I also note that the code you supplied produces the output:

2015/02/18|19:02:41|4|4|EDI|CRONSCHD|ngf_gep_outb.cron Successfully Completed
2015/02/19|11:32:27|5|4|EDI|TECPTEST|Successfully completed -------------Summary:------------------- Files received to be processed : 1 Files successfully sent to chase: 1 ------------------------------------------
2

with a 2 at the end; not the 1 that you said it produced. Looking at the code you have here and in your first post in this thread, it appears that if field 3 is less than 2, you want to that line to be considered as OIK; if field 3 is 2 or 3, you want that line to be treated as a warning; and if field 3 is greater than or equal to 4, you want that line to be treated as critical. Your sample input has two lines, one with field 3 set to 4 and one with field 3 set to 5 ; both of which you seem to want to be treated as critical, but you say you want the output to be 1:OK instead of 2:CRITICAL ???

Making lots of wild guesses, maybe the following will provide some insight that will help you figure out what will work for what you want to do:

awk -F'|' '
NF {	print	# Not an empty line; print it.
	if($3 < 2)
		o++	# $3 < 2: OK
	else if($3 < 4)
		w++	# 2 <= $3 < 4: WARNING
	else	c++	# $3 >= 4: CRITICAL
}
END {	# Print results:
	printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c)
}' datafile.txt

which, with your sample input file produces the output:

2015/02/18|19:02:41|4|4|EDI|CRONSCHD|ngf_gep_outb.cron Successfully Completed
2015/02/19|11:32:27|5|4|EDI|TECPTEST|Successfully completed -------------Summary:------------------- Files received to be processed : 1 Files successfully sent to chase: 1 ------------------------------------------
0:OK 0:WARNING 2:CRITICAL
1 Like

thank you so much Don Cragun. You made the right guesses and it's now working as it should. Thank you so much!!!