Getting syntax error with awk ternary operator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting syntax error with awk ternary operator
# 1  
Old 04-24-2015
Getting syntax error with awk ternary operator

Code:
split($7,a," "); date = a[1]; time = a[2]  
split(date,d,"/"); month = sprintf("%02d",d[1]); day = sprintf("%02d",d[2]); year = 2000 + d[3] % 100
split(time,t,":"); hour=t[1]; min=t[2]
hour >= 12? { hour=hour-12; amPm=" PM" } : amPM=" AM"
hour == 0? hour=12
time=sprintf("%02d",hour)":"sprintf("%02d",min)amPm

Code:
awk: cmd. line:9:                      hour >= 12? { hour=hour-12; amPm=" PM" } : amPM=" AM"
awk: cmd. line:9:                                  ^ syntax error
awk: cmd. line:9:                      hour >= 12? { hour=hour-12; amPm=" PM" } : amPM=" AM"
awk: cmd. line:9:                                                               ^ syntax error
awk: cmd. line:11:                      hour == 0? hour=12
awk: cmd. line:11:                                        ^ unexpected newline or end of string

I've tried seperating lines, adding more semicolons at the ends of the line or before the curly bracket BASH-style. Nothing seems to work.
I have other ternary statements in the code that work fine like:
Code:
$19 == "V"? valid = "0" : valid = $19
$22 == ""? comment = "0" : comment = $22

Mike
# 2  
Old 04-24-2015
Getting which syntax error?

The ternary operator returns its decision as a value, i.e. return( (x<0)?(-x):x ). If it doesn't make sense as an expression you can't cram it in there. { } blocks don't belong there.

I think you can put a function in there.

Code:
time = (hour >= 12) ? sprintf("%02d PM", hour-12) : sprintf("%02d AM", hour);


Last edited by Corona688; 04-24-2015 at 05:54 PM..
# 3  
Old 04-24-2015
Not sure what you mean by which syntax error? I posted the entire awk error output, there was nothing else.

OK, so I seperated it out into single expressions:
Code:
hour >= 12? amPm=" PM" : amPm=" AM"
hour >= 12? hour = hour - 12
hour == 0? hour = 12

The first line works, but the second two do not.
Code:
awk: cmd. line:11:                      hour >= 12? hour = hour - 12
awk: cmd. line:11:                                                  ^ unexpected newline or end of
awk: cmd. line:12:                      hour == 0? hour = 12
awk: cmd. line:12:                                          ^ unexpected newline or end of string

Is there some reason it works for strings and not for numbers?

I see your edit while I was posting. I don't think your code example handles midnight correctly, which is the reason I did not try to do it on one conditional.

00:00 needs to become 12:00 AM not 00:00 AM.

I'm just as interested in understanding why it is not working than finding working code.

Mike

---------- Post updated at 02:15 PM ---------- Previous update was at 02:06 PM ----------

Trying something else:
Code:
amPm= hour >= 12? " PM" : " AM"
hour= hour - hour >= 12? 12 : 0
hour= hour == 0? hour = 12 : hour

No errors. I need to ckeck the output.

So if that is the right way to use it, why do the string lines do exactly what is expected of them?
Code:
hour >= 12? amPm=" PM" : amPm=" AM"
$19 == "V"? valid = "0" : valid = $19
$22 == ""? comment = "0" : comment = $22

Mike

Last edited by Michael Stora; 04-24-2015 at 06:21 PM..
# 4  
Old 04-24-2015
Hi,
ternary operator is:
Code:
Condition ? one and only one true expr : one and only one false expr

you must have false expr also:
Code:
hour >= 12? amPm=" PM" : amPm=" AM"
hour >= 12? hour = hour - 12 : 0
hour == 0? hour = 12 : 0

Regards.
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 04-24-2015
Quote:
Originally Posted by disedorgue
you must have false expr also
Ahhhh . . . . Smilie

Code:
split($7,a," "); date = a[1]; time = a[2]
split(date,d,"/"); month = sprintf("%02d",d[1]); day = sprintf("%02d",d[2]); year = 2000 + d[3] % 100
split(time,t,":"); hour=t[1]; min=t[2]
amPm= hour >= 12? "PM" : "AM"
hour= hour - ( hour >= 12? 12 : 0 )
hour= hour == 0? hour = 12 : hour    # To handle midnight
time= sprintf("%02d",hour) ":" sprintf("%02d ",min) amPm

Mike

---------- Post updated at 03:13 PM ---------- Previous update was at 02:27 PM ----------

An afterthought . .. we can do nothing after the :
Code:
split($7,a," "); date = a[1]; time = a[2]
split(date,d,"/"); month = sprintf("%02d",d[1]); day = sprintf("%02d",d[2]); year = 2000 + d[3] % 100
split(time,t,":"); hour=t[1]; min=t[2]
amPm= hour >= 12? "PM" : "AM"
hour >= 12? hour = hour - 12 : ""
hour == 0? hour = 12 : "" #    To handle midnight
time= sprintf("%02d",hour) ":" sprintf("%02d ",min) amPm


Last edited by Michael Stora; 04-24-2015 at 06:37 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk if else syntax error

Could somebody gently point out the error of my ways in the below (the flu I'm fighting might be contributing to my current haplessness) awk -F="\t" \ '{ for (i = 1; i <= NR; i++); FNR == i; { if (length($3) < 56 && length($1) > 56) $1=($1" "$2); $2=$3; $3=$4; ... (1 Reply)
Discussion started by: Andrew767
1 Replies

2. UNIX for Dummies Questions & Answers

syntax error: invalid arithmetic operator

hi, i have a bash script that i want to receive a a string from another bash file. But because the string has a dot in the middle it gives me an error. The error is in this line: let valor=$1 and the value passed is rules.txt the error is: let: valor=rules.txt: syntax error: invalid... (2 Replies)
Discussion started by: limadario
2 Replies

3. Shell Programming and Scripting

Syntax error using Awk

more report2.txt how to modify the AWK to print above out put for given n no of inputs using report2.txt file? out put should be (3 Replies)
Discussion started by: kanakaraju
3 Replies

4. Shell Programming and Scripting

OR operator syntax question in AWK script

Hi guys, I confused about syntax used in OR script as follow: I have this sample file separated by "|" containing: January|Month No. 1 February|Month No. 2 March|Month No. 3 April|Month No. 4 May|Month No. 5 June|Month No. 6 July|Month No. 7 August|Month No. 8 September|Month No. 9... (11 Replies)
Discussion started by: cgkmal
11 Replies

5. Shell Programming and Scripting

zsh ternary operator syntax help

Hi, Can someone give me an example of how to use zsh's ternary operator? I tried: # a=1 # c=( a ? "true" : "false" ) and got: zsh: no matches found: ? I'm running zsh 4.2 on RHEL AS 4. Thanks! Paul (1 Reply)
Discussion started by: paul99
1 Replies

6. Shell Programming and Scripting

syntax error in shell test: unknown operator

Hi All, can some one figure out the syntax issue here. How to overcome this? #!/bin/sh $ HFR_MAIL=NO $ PRP_MAIL=NO $ MC_MAIL=NO $ if && && ]; then > echo "NO " > else > echo "YES" > fi test: unknown operator NO $ if && && ]; then > echo "NO" > else > echo "YES" >... (4 Replies)
Discussion started by: shellscripter
4 Replies

7. Shell Programming and Scripting

syntax error: `-a' unexpected operator/operand in IF

When i tyr this, it gives me a syntax error...i tried removing quotes,removing spaces,replacing -eq with '='.. Can somebody suggest that is the problem? if ]; then (4 Replies)
Discussion started by: dba.admin2008
4 Replies

8. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

9. UNIX for Dummies Questions & Answers

'awk' syntax error

Due to some syntax error, my below code is not working. #!/usr/bin/ksh nawk ' BEGIN { cur_val=0; cur_zero=0; cur_nine=0; sum_zero=0; sum_nine=0; } /^/ { cur_val=substr($0,5,2); if("cur_val" == "0") { ... (3 Replies)
Discussion started by: lokiman
3 Replies

10. Programming

ternary operator

How do I interpret the following ternary operation? fn_max(var_type a,var_type b,var_type c) { var_type t; return(t=((t>a?:t;a)>b)?:t;b)>c?:t;c) } Thanks (1 Reply)
Discussion started by: naan
1 Replies
Login or Register to Ask a Question