awk help : if then else not working!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk help : if then else not working!
# 1  
Old 04-23-2013
awk help : if then else not working!

Hi All,

This code is not working when trying with "else" : Am I missing something,
Code:
uptime|sed 's/,/ /g' | awk '{ if ($10 >1) { print ( " :: UPTIME GT 1 : ALERT" )} ;else print "OK" }'

Getting this error:
Code:
syntax error The source line is 1.
 The error context is
                { if ($10 >1) { print ( " :: UPTIME GT 1 : ALERT" )} >>>  ;else <<<  print "OK" }
 awk: The statement cannot be correctly parsed.



When I am trying without else it is working:
Code:
uptime|sed 's/,/ /g' | awk '{ if ($10 >1) { print (" :: UPTIME GT 1 : ALERT" )}}'

Please advise what is wrong:

Thanks,

Last edited by rveri; 04-23-2013 at 04:28 PM..
# 2  
Old 04-23-2013
The code your error shows and the code you show is not the same, which is right?

I don't think you need the ; before the else.
# 3  
Old 04-23-2013
The problem lies here:
Code:
uptime|sed 's/,/ /g' | awk '{ if ($10 >1) { print ( " :: UPTIME GT 1 : ALERT" )} ;else print "OK" }'

It should be:
Code:
uptime|sed 's/,/ /g' | awk '{ if ($10 >1) { print ( " :: UPTIME GT 1 : ALERT" );} else print "OK" }'

Or you could do:
Code:
uptime | awk -F, '{ gsub(/.*load average: /,x); if ($1>1) print ":: UPTIME GT 1 : ALERT"; else print "OK" } '

This User Gave Thanks to Yoda For This Post:
# 4  
Old 04-23-2013
Yoda,
You got it. Thanks a lot. I was struggling for a long time on the code.
Able to fix now. and Thanks for the 2nd one as a bonus.


Code:
uptime|sed 's/,/ /g' | awk '{ if ($10 >1) { print ( " :: UPTIME GT 1 : ALERT" );} else print "OK" }'
OK

Have a great day.
Reveri.

---------- Post updated at 05:42 PM ---------- Previous update was at 03:26 PM ----------

Hi All,
The comparison is not working,

When
Code:
load average: 0.25  0.25  0.27   $10 > 1 is  showing true.

This is strange.

Do I need to put
Code:
if ($10 >1.00 )

instead
Code:
if ($10 >1 )


Thanks a lot.
# 5  
Old 04-23-2013
Try adding zero to treat it as number:
Code:
$10 += 0; if ( $10 > 1 )

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: System command not working in awk

Hi, I have around 10 files in a folder in which I want to change the file format from tab(\t) to pipe(|) with some changes in the fields as well. Below is the code, while tmp file is getting generated but move command is not working, please help Following is the code awk -F"\t" '{print... (2 Replies)
Discussion started by: siramitsharma
2 Replies

2. Shell Programming and Scripting

awk - why this is not working? trying next word!

Hi Experts, Can you please advise , why I am not able to make it work, or why this is not working: I spent quite a lot of time on this figuring out , but not working, file : This is a test file thanks for your reply This is another file again Have a nice day this is a small file... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

awk not working correctly

Hi I am attempting to right a script which will read a table and extract specfic information. LASTFAILEDJOB=/usr/openv/netbackup/scripts/GB-LDN/Junaid/temp_files/lastfailedjob cat /usr/openv/netbackup/scripts/GB-LDN/Junaid/temp_files/lastfailedjob 237308646 If i run the following... (5 Replies)
Discussion started by: Junes
5 Replies

4. Shell Programming and Scripting

awk isn't working

awk 'BEGIN{print '1.2449'**0.5}' awk: line 1: syntax error at or near * can someone help me troubleshoot the above command? i'm trying to do the square root of 1.2449. this command works on Red Hat, but for some reasonn isn't working on kubuntu (latest version). shell is bash. i... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Shell Programming and Scripting

[awk] working with two files

Hello guys, I have little to no experience working with two files in awk and hope you can help me with a problem that may be easy for you to solve... awk -v cut1="$var1" -v cut2="$var2" '{split($0, arr1); for(i=1;i<=NF;i++) if (arr1 < cut1) print arr1, NR, i}' file1 file2 The above code is... (4 Replies)
Discussion started by: origamisven
4 Replies

6. UNIX for Advanced & Expert Users

Awk expressions working & not working

Hi, Putting across a few awk expressions. Apart from the last, all of them are working. echo a/b/c | awk -F'/b/c$' '{print $1}' a echo a/b/c++ | awk -F'/b/c++' '{print $1}' a echo a/b/c++ | awk -F'/b/c++$' '{print $1}' a/b/c++ Request thoughts on why putting a '$' post double ++... (12 Replies)
Discussion started by: vibhor_agarwali
12 Replies

7. Shell Programming and Scripting

awk command not working

Hi all, Trying to write a script that reads a file and prints everything after a certain string is found to the end of the file. Awk is giving me an error and not sure why it doesn't work: # cat test_file Mon Nov 16 2009 16:11:08 abc def Tue Nov 17 2009 16:08:06 ghi jkl Wed Nov 18... (8 Replies)
Discussion started by: jamie_collins
8 Replies

8. UNIX for Dummies Questions & Answers

Logical AND, OR not working with awk.

awk -F^ '{ if ((($1 == "M") && ($5 == "2")) || (($1 == "S") && ($5 == "7"))) print $0}' welcome > welcome1 When I run the above awk command, I could see the same content of welcome in welcome1. Later, I found that the logicals "&&", "||" used in the above command is not working. Can any... (2 Replies)
Discussion started by: karthickrn
2 Replies

9. Shell Programming and Scripting

awk not working in script

Hi All, i'm trying awk commnad to read only file names with the help of below command :- ls -l tmp*.txt | awk { 'print $9' } | head -1 The command is working fine :o on command line, but when i trying it in script then its not working $var1=`ls -l tmp*.txt | awk { 'print $9' } | head -1`... (3 Replies)
Discussion started by: doitnow
3 Replies

10. Shell Programming and Scripting

awk not working for me.

Hi All, I have a server.xml file which looks something like. <Connector port="8443" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" disableUploadTimeout="true" acceptCount="100"... (2 Replies)
Discussion started by: nua7
2 Replies
Login or Register to Ask a Question