Sponsored Content
Full Discussion: Convert a String to a Number
Top Forums Shell Programming and Scripting Convert a String to a Number Post 302994037 by jim mcnamara on Friday 17th of March 2017 07:48:10 PM
Old 03-17-2017
@scrutinizer - you are correct. Look below.

Actually, my point was not about anything that uses validation. That whole thing was a trip down a rabbit hole.

The OP insisted that there was no way that a variable could be treated as "polymorphic", I showed two shell examples to the contrary. i.e., 01 and 1 would register as 1 in
Code:
 [ $myvar -eq 1 ]

The only conclusion was that OP was using an ancient shell, so I suggested typeset. Then we all went down a rabbit hole. Smilie We are still there.

To answer your question in the OP's context let's see what ksh88 on Solaris 10 "thinks":

Code:
 for i in "-1" "+1" "01" "1" " 1"
 do
        
    printf "%%s=%s: %%d=:%d: " $i $i  
    [ $i -eq 1 ] && echo "Numeric and equals 1"
    [ $i -eq -1 ] && echo "Numeric and equals -1"
    [ $i -eq 0 ] && echo "probably not numeric"
 done

output:
Code:
%s=-1: %d=:-1: Numeric and equals -1
%s=+1: %d=:1: Numeric and equals 1
%s=01: %d=:1: Numeric and equals 1
%s=1: %d=:1: Numeric and equals 1
%s=1: %d=:1: Numeric and equals 1

Now let's compare that to bash and bash plus RE - note change to RE

Code:
#!/bin/bash
for i in "-1" "+1" "01" "1" " 1"  "-000001"
do
 printf "%%s=:%s: %%d=:%d: " $i $i  
 [ $i -eq 1 ] && echo "Numeric and equals 1"
 [ $i -eq -1 ] && echo "Numeric and equals -1"
 [ $i -eq 0 ] && echo "probably not numeric"
done

echo 'using RE'
regex='^([+-]{0,1}[0-9]+$| [0-9]+$|[0-9]+$)'

for i in "-1" "+1" "01" "1" " 1" "-0000001"
do
   printf "%%s=:%s: %%d=:%d: " $i $i  
   if [[ $i =~  $regex ]] ; then 
         echo 'tests numeric '  
   else
         echo 'tests not numeric'
   fi      
done

output:
Code:
%s=:-1: %d=:-1: Numeric and equals -1
%s=:+1: %d=:1: Numeric and equals 1
%s=:01: %d=:1: Numeric and equals 1
%s=:1: %d=:1: Numeric and equals 1
%s=:1: %d=:1: Numeric and equals 1
%s=:-000001: %d=:-1: Numeric and equals -1
using RE
%s=:-1: %d=:-1: tests numeric
%s=:+1: %d=:1: tests numeric
%s=:01: %d=:1: tests numeric
%s=:1: %d=:1: tests numeric
%s=:1: %d=:1: tests numeric
%s=:-0000001: %d=:-1: tests numeric

Hmm. ksh88 is the oldest shell I have. So none of the other stuff is really needed to do what the OP appears to have wanted, IMO. This was my original point. Your point is that signed numbers fail the RE in my example, which is true. My bad. Fixed.

All of this might have been over the OP's level of understanding anyway.
This User Gave Thanks to jim mcnamara For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert ip address to ip number

I want to make a script to read a list of ip addresses from a file then convert those ip addresses to ip number. ip number is made by removing the dots then transfer to a number , so the ip number transfered to binary then to decimal which will represents the ip number 0 : 4294967295 Also I... (17 Replies)
Discussion started by: moutaz1983
17 Replies

2. Shell Programming and Scripting

need script to convert number in hexadecimal

hi , i need a script to convert number into hexadecimal base for example: 237=>ED it s very important for me thank you in advance for you help (5 Replies)
Discussion started by: mips
5 Replies

3. Shell Programming and Scripting

Convert e+ to number

Hi, I am using awk to get particular dates in seconds and the output am getting is like 1.28071e+09. How can I convert it to number format. Can anyone help me out? Thanks in advance..! (7 Replies)
Discussion started by: Kattoor
7 Replies

4. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

How to convert number to english?

Hi gurus, I have a weird requirement. I need to convert the number to english lecture. I have 1.2 ....19 numbers I need to convert to first second third fourth, fifth, sixth... Is there any way convert it using unix command? thanks in advance. (8 Replies)
Discussion started by: ken6503
8 Replies

7. Shell Programming and Scripting

Convert number

friends as I can convert this value to number in this example it is well but can vary the value does not help me cut from a nesecito espefifica opsiocn get zero CantCabe = 00023 Cant = 23 CantPntCabe = 0000000000000034 CantPnt = 34 if && ; then echo... (3 Replies)
Discussion started by: tricampeon81
3 Replies

8. Shell Programming and Scripting

Convert string number to a integer

I have data as below "ROWS merge process complete. thousand rows changed" I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000. Any help or pointer. Please use CODE tags as required by forum rules! (6 Replies)
Discussion started by: dsravanam
6 Replies

9. Shell Programming and Scripting

Convert floating point to a number

Hello Guys, I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value #!/bin/bash #... (9 Replies)
Discussion started by: skatpally
9 Replies

10. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies
All times are GMT -4. The time now is 06:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy