Convert string to numeric


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert string to numeric
# 1  
Old 11-13-2003
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  
Old 11-14-2003
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  
Old 11-19-2003
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  
Old 11-19-2003
OK Good

ΊΓΡω΅Δ
Go on
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. Shell Programming and Scripting

Convert Numeric Time to Readable Timestamp - Perl

I am trying to hit an URL using below command and get the data into an excel sheet. wget --user=<<USERID>> --pass=<<PASSWROD>> http://www.files.thatbelongstome.com/file1 -O test1.xls Next step is to consolidate files from 1 to 10 in a single excel sheet and send to my mail. I am working on... (1 Reply)
Discussion started by: PikK45
1 Replies

3. Shell Programming and Scripting

Extract all first numeric character from a string

Hello, I have a file of strings a below:- 4358RYFHD9845 28/COC/UYF984 9834URD 98HJDU I need to extract all the first numeric character of every sting as follows:- 4358 28 9834 thanks to suggest ASAP Regards, Jasi Use code tags, thanks. (7 Replies)
Discussion started by: jassi10781
7 Replies

4. Programming

C++ Get text and numeric value from a string

I have a string opt="row234" I want to put "row" in a string and 234 in an int. In general it should be opt="textnum" I want to store text in a string and num in an int. (6 Replies)
Discussion started by: kristinu
6 Replies

5. Shell Programming and Scripting

check if a string is numeric

I checked all the previous threads related to this and tried this. My input is all numbers or decimals greater than zero everytime. I want to check the same in the korn shell script. Just validate the string to be numeric. This is what I am doing. var="12345" if ) -o "$var" !=... (14 Replies)
Discussion started by: megha2525
14 Replies

6. Shell Programming and Scripting

How to parse a numeric string without any delimiters?

Hi , I have a number say 12345001 which needs to be parsed. Its a number that has no delimiters.I have to read the last three digits and then the rest of digits irrespective of the total length of the number. The digits then have to be swapped and changed to a fixed length. The fillers to be... (10 Replies)
Discussion started by: Sheel
10 Replies

7. Shell Programming and Scripting

Extracting numeric from string

I have a file whose contents are: $ cat file1 cfd_V03R37 cfd_V03R38 tried sed 's///g' file1 > file2 $cat file1 0337 0338 Is there any way by which i can work on same file and write o/p to the same file instead of using file2 (3 Replies)
Discussion started by: vjasai
3 Replies

8. Programming

check the given string is numeric or not.

Hi, how to check the given string is numeric or not , without converting ( using strtol...). for ex: if string is C01 - non-numeric data if string is 001 - numeric data TIA (11 Replies)
Discussion started by: knowledge_gain
11 Replies

9. Shell Programming and Scripting

numeric string and length

given a string passed to a program that supposed to be numeric and of a certain length say 8 digits - so say for e.g. need to verify this 01234567 How would I parse this string to validat it meet requirements I tried to use * | sed /\(\{8})/ Thanks in advance (1 Reply)
Discussion started by: dragrid
1 Replies

10. UNIX for Dummies Questions & Answers

To convert the numeric month into alphabetic

hi , This function actually gives me the last month but as a numeric value what should i do to get it as suppose :Jul date '+%b %Y' | { read MONTH YEAR MONTH=`expr "$MONTH" - 1` echo $MONTH } Thanks. Rooh (2 Replies)
Discussion started by: rooh
2 Replies
Login or Register to Ask a Question