The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check the given string is numeric or not. knowledge_gain High Level Programming 11 02-03-2009 11:25 AM
problem in comparing numeric with string naren_0101bits Shell Programming and Scripting 7 01-29-2008 11:57 AM
Perl code to differentiate numeric and non-numeric input Raynon Shell Programming and Scripting 11 08-04-2007 10:32 AM
With Regex Spliting the string into Alphanumeric and Numeric part ozgurgul Shell Programming and Scripting 1 06-30-2007 09:52 AM
To convert the numeric month into alphabetic rooh UNIX for Dummies Questions & Answers 2 08-20-2001 03:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-13-2003
kflee2000 kflee2000 is offline
Registered User
  
 

Join Date: Nov 2003
Posts: 1
Convert string to numeric

Hi,
I have read some figures from a text file by getting the position and wish to do some checking, but it seem like it won't work.

eg. my figure is 0.68 it still go the the else statement,
it seems like it treat it as a text instead of number.

Anybody can Help ? Thanks.

# only one line
cat $outfile | while read line
do
f1=$(echo $line|cut -c 14-17)
f2=$(echo $line|cut -c 19-22)
f3=$(echo $line|cut -c 24-27)
done

echo $f1 $f2 $f3
a=$f1
x=0.5
if [ $a -gt $x ]
then
echo 'Grater ' $f1 $f2 $f3
else
echo 'is ok :'
fi
  #2 (permalink)  
Old 11-14-2003
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
I'm afraid that you can only do integer arithmetic and comparisons via the shell. One way to get around this is to use a utility like awk or bc to multiply your variables by the same amount, eg....

a=0.89
x=0.5
a=$(echo $a|awk '{print $1*1000}')
x=$(echo $x|awk '{print $1*1000}')

if [ $a -gt $x ]
then
echo $a Greater than $x
fi

Gives...

890 Greater than 500
  #3 (permalink)  
Old 11-19-2003
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
Hi,

You can directly use awk

echo $i $j|awk '
{
if ( $1 > $2 )
print $1 " Greater than " $2 "\n"
else
print $1 " Smaller than " $2 "\n"
}'
  #4 (permalink)  
Old 11-19-2003
wjackm wjackm is offline
Registered User
  
 

Join Date: Nov 2003
Posts: 1
OK Good

ΊΓΡω΅Δ
Go on
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0