if condition not evaluating as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if condition not evaluating as expected
# 1  
Old 08-05-2010
Question if condition not evaluating as expected

Hi all,

Thanks in advance for your time.

I have a data file like this:

1 7.465753425
2 8.980821918
1 1.717808219
1 6.550684932
0 5.432876712

I wish to write a bash script to check both columns and output a 1 if col1==1 AND col2<3. Otherwise I want to output a 0. In the above example, the result would be:

0
0
1
0
0

My script:

Code:
inputfile=events
outputfile=surv3yrs

while read line
do

type= echo $line | awk '{print $1}'
time= echo $line | awk '{print $2}'

echo $type
echo $time

if [[ $type -eq 1 && $time -lt 3 ]]; then
	echo 1 >> $outputfile
else 
	echo 0 >> $outputfile
fi

done < $inputfile

The echo $type and echo $time statements are in there to ensure the file is read correctly, which it seems to be. However my results are all 0s:

0
0
0
0
0

So my condition statement is never evaluating to true. Can anyone explain why?

Also if there's a better solution to this problem I'd be glad to here it - output can be in another file (as above) or recoded into another column of the same file, I don't really care.

Many thanks,
Jen
# 2  
Old 08-05-2010
hi ,
Something like this ?

Code:
 awk '{if ($1==1 && $2 < 3) { print "1"} else {print "0"}}' inputfile

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 08-05-2010
Code:
$ awk '{print ($1==1&&$2<3)?1:0}' $inputfile
0
0
1
0
0

Here's an updated version of your script:

Code:
while read type time rest_of_line
do

echo $type
echo $time

if [[ $type -eq 1 && $time -lt 3 ]]; then
        echo 1 >> $outputfile
else
        echo 0 >> $outputfile
fi

done < $inputfile

# 4  
Old 08-05-2010
Many thanks to both of you for your solutions.

Code:
awk '{print ($1==1&&$2<3)?1:0}' $inputfile

Works a treat, and is much simpler - thank you scottn.

The re-worked code you posted gives me many similar error messages:

Code:
./convertevents.sh: line 16: [[: 7.465753425: syntax error: invalid arithmetic operator (error token is ".465753425")

I don't think it's reading the real number (7.465....) properly. Any thoughts? I'll be using the simpler awk code but I'd like to understand the read line function as well, for future reference.

Cheers.
# 5  
Old 08-05-2010
Hi.

What shell are you using, on what OS?

Try this, but going by the error, it's probably much the same:

Code:
if [ $type -eq 1 -a $time -lt 3 ]; then

Shells don't usually handle floating point arithmetic (the number will get truncated), which makes your if problematic.

Code:
$ a=3.3
$ b=3.2
$ [ $a -gt $b ] && echo a is greater than b
$ (no output)


Last edited by Scott; 08-08-2010 at 08:23 AM.. Reason: changed && to -a
# 6  
Old 08-05-2010
bash, Mac OS 10.5

It's fine, I think understand the problem now, and I don't need to get this particular code working anyway as I've used the more elegant awk solution.

Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem evaluating condition

First, given this bit of code (line numbers included for reference: 59 get_all_db () { 60 #echo getting all db 61 dblist=`egrep -i "product/12" /etc/oratab |grep -v "listener"|\ 62 awk -F\: '{print $1}'|sort` 63 echo list is $dblist 64 echo 65 echo 66 echo "INSTANCE_NAME ... (15 Replies)
Discussion started by: edstevens
15 Replies

2. Shell Programming and Scripting

unary operator expected, if condition test error.

Hi All, I'm assigning a numeric value to variable count=2, well its being assigned by code above the if condition. I want to test for 2 conditions , when $count = 0 or $count <=2 and do something when the condition matches. here is my code, but i run into the infamous : if ] then ... (2 Replies)
Discussion started by: Irishboy24
2 Replies

3. Shell Programming and Scripting

if condition error: integer expression expected

I am trying to run following condition with both variables having numeric values "1,2,3" if ;when i run it i get following error: $NEW_STATE: integer expression expected Please correct me where I'm doing wrong. I'm trying to check either New State is greater or Old state.... (0 Replies)
Discussion started by: kashif.live
0 Replies

4. Shell Programming and Scripting

if condition-integer expression expected

Hi , getting error in below code integer expression expected. its due to floating point. plz help me to solve this ADV1=94.3 Quantity=96.3 if ; then echo "quantity is greter" fi (6 Replies)
Discussion started by: aish11
6 Replies

5. UNIX for Dummies Questions & Answers

Comparing and Evaluating

Hi Guys, Good day ULF :) I hope you can help me again with my problem. I have a file which looks like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server3 Dec 18 21:31:12 AHM04 nseventmgr: EVENT-SET:... (4 Replies)
Discussion started by: rymnd_12345
4 Replies

6. Shell Programming and Scripting

Evaluating a variable

Does anyone know of a way to force a variable name held in another variable to return the value of the first variable? Best if I give an example, that does not work: /usr/local/bin >cat mike.sh NUM1ref=16 NUM2ref=32 echo "==============" for VAR in NUM1 NUM2 do XXXX=${VAR}ref echo $XXXX... (4 Replies)
Discussion started by: mikejordan
4 Replies

7. Shell Programming and Scripting

$$# is evaluating to 1 when no value

I have the following in my makefile: RESULT=`../${TOOLS_ROOT_PATH}/ext_tools.sh 11`; \ set $$RESULT > tMp; \ rm tMp; \ if ; then \ echo copying external-local tool: $< \($$*\); \ mkdir -p ${EXTERNAL_LOCAL_BIN_DIR}/$<; \ cp -f... (4 Replies)
Discussion started by: jake_ryan
4 Replies

8. UNIX for Dummies Questions & Answers

evaluating date +%m

how do i evaluate the value of date if ( $(date +%m) > 8 ) then FY_STAMP=FY$(echo $(($(date +%Y) + 1)) | cut -c3-4) else FY_STAMP=FY$(date +%y) fi i want this to make the FY_STAMP increment by 1 if the month is september and up. but cant seem to make it work (3 Replies)
Discussion started by: rsf01
3 Replies

9. Shell Programming and Scripting

* character evaluating too soon - Help!

I have a user defined configuration file, which could contain the following type of entries: directory_001=/a/directory/structure pattern_001=fred* pattern_002=* I have a script which reads the file generically which will loop round loop 1 genvar=”directory” iteration=”001” ... (11 Replies)
Discussion started by: Bab00shka
11 Replies

10. UNIX for Dummies Questions & Answers

evaluating params

Hi all, I ve a script like.... TBL=employee sql=`cat abhi.sql` \\ abhi.sql contains ------- select a from $TBL echo $TBL echo $sql SQL=`echo $sql` echo $SQL now i want SQL as select a from employee and as select a from $TBL How can I achieve this? Help appriciated (3 Replies)
Discussion started by: abzi
3 Replies
Login or Register to Ask a Question