Visit Our UNIX and Linux User Community


Awk or If/statement Calculation Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk or If/statement Calculation Problem
# 1  
Old 09-23-2012
Awk or If/statement Calculation Problem

Code:
#!/bin/sh

CURRENTSTATE=2
CSVCSTATE=2
LASTSTATECHANGE=8
CSVCSTATEAGE=5

if (($CURRENTSTATE==$CSVCSTATE))&&(($LASTSTATECHANGE>=$CSVCSTATEAGE))

echo GREAT

fi

returns:

Code:
./aff: line 12: syntax error near unexpected token `fi'
./aff: line 12: `fi'

what am i doing wrong here?

sometimes, when the numbers are in notation, i also get an error. how can i fix this?

i get this error:

Code:
script.sh: : [: 1.51764e+06: integer expression expected

i want to be able to compare any value to a set number.

like, if 1.51764e+06 is greater than whatever value is in a specific variable, do this or that.

any ideas?
# 2  
Old 09-23-2012
I am confused why you call this thread 'awk' when you're not using awk.

You didn't put a 'then', shell if needs those.

Code:
if [ "$NUMBER" -lt "$OTHERNUMBER" ]
then
        echo "something"
fi

...but, as you discovered, shell does not support floating point. KSH is the only shell that does.

You can use awk instead, but this is expensive.

Code:
if awk -v VAR1="$VAR1" -v VAR2="$VAR2" 'END { if(!(VAR1<VAR2)) exit 1}' /dev/null
then
        echo "var1 < var2"
fi

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-23-2012
Quote:
Originally Posted by Corona688
I am confused why you call this thread 'awk' when you're not using awk.

You didn't put a 'then', shell if needs those.

Code:
if [ "$NUMBER" -lt "$OTHERNUMBER" ]
then
        echo "something"
fi

...but, as you discovered, shell does not support floating point. KSH is the only shell that does.

You can use awk instead, but this is expensive.

Code:
if awk -v VAR1="$VAR1" -v VAR2="$VAR2" 'END { if(!(VAR1<VAR2)) exit 1}' /dev/null
then
        echo "var1 < var2"
fi

thank you for the response!

the script i'm working on is pretty huge. but here is the part i just need to modify to be able to support a floating point:

Code:
if (( ${CURRENTSTE} == ${CSVCSTE} ) && ( ${LASTSTECHANGE} -ge ${CSVCSTEAGE} )) ; then

any one of these variables can have a floating point.

the script is written in bash.
# 4  
Old 09-23-2012
If any one of those variables can have a floating point, you can't use ==. An infintesimal difference or even same number, different representation could make them not equal. How accurate do you want the comparison?
# 5  
Old 09-24-2012
Quote:
Originally Posted by Corona688
If any one of those variables can have a floating point, you can't use ==. An infintesimal difference or even same number, different representation could make them not equal. How accurate do you want the comparison?
this is what i ended up doing, but i'm not sure if it's doing what i want it to:

Code:
                if awk -v CURRENTSTE="$CURRENTSTE" \
                       -v CSVCSTE="$CSVCSTE" \
                       -v LASTSTECHANGE="$CSVCSTEAGE" \
                       -v CSVCSTEAGE="$CSVCSTEAGE" \
                'END { if(!(CURRENTSTATE==CSVCSTE) && (LASTSTECHANGE>=CSVCSTEAGE)) exit 1}' /dev/null ; then

# 6  
Old 09-24-2012
Quote:
Originally Posted by SkySmart
Code:
                if awk -v CURRENTSTE="$CURRENTSTE" \
                       -v CSVCSTE="$CSVCSTE" \
                       -v LASTSTECHANGE="$CSVCSTEAGE" \
                       -v CSVCSTEAGE="$CSVCSTEAGE" \
                'END { if(!(CURRENTSTATE==CSVCSTE) && (LASTSTECHANGE>=CSVCSTEAGE)) exit 1}' /dev/null ; then

can we use above highlighted directly..?

i think this should be

Code:
if [[ $(code) ]]
then
#do some here
fi

# 7  
Old 09-24-2012
Quote:
Originally Posted by pamu
can we use above highlighted directly..?
Yes, this is how if is meant to be used. It checks the return status of the command given.

If in doubt, remember, [ is a command.

Code:
$ whereis '['

[: /usr/bin/[

$

Usually a shell builtin, these days, but the command still exists.
This User Gave Thanks to Corona688 For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk calculation with zero as N/A

In the below awk, I am trying to calculate percent for a given id. It is very close the problem is when the # being used in the calculation is zero. I am not sure how to code this condition into the awk as it happens frequently. The portion in italics was an attempt but that lead to an error. Thank... (13 Replies)
Discussion started by: cmccabe
13 Replies

2. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. 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

4. Shell Programming and Scripting

Awk/sed problem to write Db insertion statement

Hi There, I am trying to load data from a csv file into a DB during our DB migration phase. I am successfully able export all data into a .csv file but those have to rewritten in terms insert statement which will allow for further population of same data in different DB My exiting csv record... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

5. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

6. Shell Programming and Scripting

Decimal number calculation problem

I have a below snippet of code from my perl script and its causing a problem when the output of $lTAX is 0 (zero) its getting displayed as -0.00. I want output to be 0 not -0.00. Any help would be appreciated. #!/usr/bin/perl my $lTotA = 50.94; my $lVatA = 8.49; my $lAllocD; my $lAdjNr =... (4 Replies)
Discussion started by: Devesh5683
4 Replies

7. Shell Programming and Scripting

Problem in echo value after some calculation

val=21 total=3250 echo "`echo "scale=2; $val*100/$total" | bc`" Output: .64 How do i show the output become "0.64" instead of ".64" ?? Someone can help? (1 Reply)
Discussion started by: alvin0618
1 Replies

8. Shell Programming and Scripting

Problem with awk and if statement

Hi, I have a task where i need to compare columns of two files. First file is $REG_InputFileName: "UPDATE","1010021126","1-01-01","USD" "UPDATE","1013000101","1-01-01","THB" "UPDATE","1013010107","1-01-01","THB" "UPDATE","1110011122","1-01-01","USD"... (5 Replies)
Discussion started by: manmeet
5 Replies

9. Shell Programming and Scripting

awk calculation problem

I have a list of coordinate data, sampled below. 54555209 784672723 I want it as: 545552.09 7846727.23 Below is my script: BEGIN {FS= " "; OFS= ","} {print $1*.01,$2*.01} This is my outcome: 5.5e7 7.8e8 How do I tell awk that I want to keep all the digits instead of outputting... (1 Reply)
Discussion started by: ndnkyd
1 Replies

10. Shell Programming and Scripting

Problem with float calculation and awk

Hi All, can some body help me to script the below logic. Basically am facing problem with float calculation. Also i need this to be done inside a single awk. I tried lot of tuning but still nothing is getting displayed, nor any errors param=50 value=19.23 for(i=0;i<4;i++) {... (2 Replies)
Discussion started by: jisha
2 Replies

Featured Tech Videos