help fixing awk statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help fixing awk statement
# 1  
Old 08-01-2012
help fixing awk statement

Code:
 awk "BEGIN {if($MessageREAD<$ThresholdW)

                {print \"OK\" ; exit 0}

        else if(($MessageREAD>=$ThresholdW) && ($MessageREAD<$ThresholdC))

                {print \"WARNING\" ; exit 1}"

        else if($MessageREAD<=$ThresholdC)

                {print \"CRITICAL\" ; exit 2} }"

can someone help me fix this? i know i'm not closing it properly. can someone fix this for me please?

thanks

shell = bash
os = linux / sunos
# 2  
Old 08-01-2012
Looks like the extra double-quote is after exit 1}.

I would suggest that you pass values from the shell on the command line, instead of expanding them within the AWK code. This would allow the use of stronger single-quotes, avoiding the clumsy and error-prone double-quote escaping, embedded shell parameter expansions, and would also protect the AWK script from characters in the shell variables' values which have special meaning to AWK.

Regards,
Alister

Last edited by alister; 08-01-2012 at 02:15 PM..
# 3  
Old 08-01-2012
Or, why use awk in this case? It can easily be done in shell:
Code:
if   [ "$MessageREAD" -lt "$ThresholdW" ]; then
  echo "OK"
elif [ "$MessageREAD" -ge "$ThresholdW" ] && [ "$MessageREAD" -lt "$ThresholdC" ]; then
  echo "WARNING"; exit 1
elif [ "$MessageREAD" -le "$ThresholdC" ]; then
  echo "CRITICAL"; exit 2
fi

I copied the conditions, but perhaps the last statement should be -ge ( greater or equal ) ?
One would need to make sure the exit is from the right shell..
# 4  
Old 08-02-2012
i opted not to use the regular if statements because it doesn't do well with numbers with decimal or with negative numbers.


CA=19.3
BE=5.0
HE=-6

awk doesn't have a problem with them
# 5  
Old 08-02-2012
Code:
awk -v MR="$MR" -v THW="$THW" -v THC="$THC" 'BEGIN {
         MR=MR+0; THW=THW+0; THC=THC+0; # convert string to float
         if(MR<THW){ print "OK"; exit }
         if((MR>=THW)&&(MR<THC)) { print "Warning"; exit 1 }
         if(MR<=THC) { print "Critical"; exit 2 }
         print "Unknown"; exit 3 }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help fixing awk code

can someone please help me spot and fix the issue with the following code: awk -F, -v SEARCHPATT="(Wed|Tue)" -v ADDISTR="Mon|Tue|Wed|Thu|Fri|Sat|Sun" -vVF="$VALFOUND" "BEGIN{ {D = D = 1 D = D = 2 } $0 ~ "," VF "," {L = 1 ... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

awk if then else statement

I am using awk as part of and if then else statement. I am trying to have the user enter a gene name and then a variant or variants and have a specific transcript assigned to the variants depending on the gene. Below is my code but the if then else statement is wrong. Basically, the gene name... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Help with awk statement

Hi I have a file with test test2 1000000657373 test1 test3 1000003849450 test2 test4 test5 100000837474 I cat the file and pipe it to an awk statement like so cat /tmp/file |awk '{if ($3 ~ "^*$" && $3 > 1024000000) print "/vol/"$1"/"$2;else if ($4 ~ "^*$" && $4 > 1024000000) print... (15 Replies)
Discussion started by: bombcan
15 Replies

5. Shell Programming and Scripting

Awk Problem - Fixing needed

os: sunos/linux shell: bash awk -v PCT="$PERCENTAGE" -v UWARN="$UWARNING" -v UCRIT="$UCRITICAL" 'BEGIN { PCT=PCT+0; UWARN=UWARN+0; UCRIT=UCRIT+0 ; if(PCT<UWARN) { printf \" '${FILESYS}':... (3 Replies)
Discussion started by: SkySmart
3 Replies

6. Shell Programming and Scripting

Help fixing awk code to print values from 2 files

Hi everyone, Please help on this: I have file1: <file title="Title 1 and 2"> <report> <title>Title 1</title> <number>No. 1234</number> <address>Address 1</address> <date>October 07, 2009</date> <description>Some text</description> </report> ... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

7. Shell Programming and Scripting

Awk 'if' statement help

Hello all, I'm very new to this (<5hrs!) please bear with me. My file looks like this 386259.448541 417069.155 154935.157 186.206 162 1 1 8 386259.448551 417068.53 154935.04 186.144 156 1 1 8 386259.448561 417067.911 154934.926 186.175 164 1 1 8 386259.450337 417086.643 154946.483 894.671... (4 Replies)
Discussion started by: rebeccab37
4 Replies

8. Shell Programming and Scripting

awk, if statement

Having a little trouble with awk and an if statement. I have a test setup which I am trying to only print the records which start with the month 03. Everything I tried, prints everything, even the 02 month 03/23/2010 12:47:51 ga2828 SUBMITTED FROM URL: test123.cgi show port count ... (2 Replies)
Discussion started by: numele
2 Replies

9. Shell Programming and Scripting

awk inside another awk statement

hi all, i have two files 1) a.txt one two three 2) abc "one" = 10 pqr "three" = 20 345 "two" = 0 this is what i want in third file (3 Replies)
Discussion started by: shishirkotkar
3 Replies

10. UNIX for Dummies Questions & Answers

if statement in awk

Hi Friends How do I do two things from one if statement inside awk? I want to run a script and create a new file from the same condition. awk '{ if ($2 == ""){print " "|"cd /local/test; ./script.ksh"}{cat > ran_true.txt}}' $IN_FILE Bolded are the two things I want to be done. Thanks (1 Reply)
Discussion started by: UNovIX
1 Replies
Login or Register to Ask a Question