awk script modification


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script modification
# 1  
Old 03-03-2015
awk script modification

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

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

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


Last edited by SkySmart; 03-03-2015 at 07:48 PM..
# 2  
Old 03-03-2015
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?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-09-2015
can someone please help me out with this:

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

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

Code:
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.
# 4  
Old 03-09-2015
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:
Code:
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:
Code:
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:
Code:
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

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 03-09-2015
thank you so much Don Cragun. You made the right guesses and it's now working as it should. Thank you so much!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modification to awk command

i have a php file that has this: php.code #!/usr/bin/php <?php phpinfo(); hlight_file(__FILE__); ?> I want my awk code grab whatever is inbetween and including the "<?php" and "?>". Then, it should scan all the entries between these two points. And if the entries between these... (10 Replies)
Discussion started by: SkySmart
10 Replies

2. Shell Programming and Scripting

awk script modification - treat certain files differently

awk 'BEGIN{OFS=","} FNR == 1 {if (NR > 1) {print fn,fnr,nl} fn=FILENAME; fnr = 1; nl = 0} {fnr = FNR} /UNUSUAL/ && /\.gz/ ~ /FILENAME/ {nl++} <'{system ("gunzip -cd FILENAME")}' END ... (2 Replies)
Discussion started by: SkySmart
2 Replies

3. Shell Programming and Scripting

IP Address Modification through awk/sed

Hi, I have to modify the 2nd and 3rd octet of the IP address through awk/sed. For Example: Given IP is : 10.205.22.254, it should be modified as 10.105.100.254 through awk/sed. Kindly help me on this and let me know if you have any questions. Thanks in advances. (2 Replies)
Discussion started by: kumarbka
2 Replies

4. UNIX for Dummies Questions & Answers

awk output modification

Hello, I am using awk command to print some output, but there are some characters that I would like to remove from the output awk '{print $5$6}' the output I get is column5/:column6 I am looking forward to remove the : and to get the output column5/column6 Sorry if this question is... (4 Replies)
Discussion started by: Error404
4 Replies

5. Shell Programming and Scripting

awk modification for lines

so i have this data in a file: jime=1860,yime=1.23243,lime= jime=1859,yime=1.23018,lime= jime=1825,yime=1.15371,lime= jime=1849,yime=1.20769,lime= jime=1841,yime=1.1897,lime= jime=1849,yime=1.20769,lime= i use this code to calculate the percentage difference of the number in column 2... (9 Replies)
Discussion started by: SkySmart
9 Replies

6. Shell Programming and Scripting

awk script modification

I want the below script to omit every chunk of data that contains a specific hostname. here's the scenario. i have a configuration file that contains the configuration of several hosts. a sample of this configuration file is this: define host { address ... (12 Replies)
Discussion started by: SkySmart
12 Replies

7. Shell Programming and Scripting

Awk modification

I need help modifying the code below. DATAFILE is a log file. I have two strings i need to search for in the log file. The two strings are: 1. ERROR 2. com.rolander.promotions.client awk 'BEGIN { while((getline < "'${SFILE}'")>0) S FS="\n"; RS="\n" } (11 Replies)
Discussion started by: SkySmart
11 Replies

8. Shell Programming and Scripting

in line modification in a file using awk

Hi, I have a conf.file with the following values: ef=78 b=40 ca=40 c=45/dev2 when I modify one of the line with the below awk script,it's modifying BUT it's placing the modified line in the last line : input:- Configure b 45/dev4 output:- ef=78 ca=40 ... (2 Replies)
Discussion started by: axes
2 Replies

9. Shell Programming and Scripting

Need a modification on this script

Hi All I have files contains rows which look like this: 2 20090721_16:58:47.173 JSUD2 JD1M1 20 IAM 966591835270 249918113182 b 3610 ACM b 3614 ACM b 3713 CPG b 3717 CPG f 5799 REL b 5815 RLC b 5817 RLC :COMMA: NCI=00,FCI=6101,CPC=0A,TMR=00,OFI=00,USI: :COMMB: BCI=1234: :RELCAUSE:10: ... (1 Reply)
Discussion started by: zanetti321
1 Replies

10. Shell Programming and Scripting

help in script modification

i have the following perl script.but it searches for a given filename. i want to run the same script in my directoy which has subdirectories too and it has to display the file if sreach satisfies along with directory name. can anyone help me: perl script: my $FILE = $ARGV; for zf in... (4 Replies)
Discussion started by: a.suryakumar
4 Replies
Login or Register to Ask a Question