Extract numbers from a string and store in variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract numbers from a string and store in variables
# 1  
Old 11-12-2007
Question Extract numbers from a string and store in variables

Hi All,

Is it possible in Unix shell script to extract numbers from a string containing ".", such as;

5.2.314

And store in variables so;

var1 = 5
var2 = 2
var3 = 314

Thanks in advance for any help anyone can provide

dave
# 2  
Old 11-12-2007
With zsh:
Code:
zsh-4.3.4% s="5.2.314"
zsh-4.3.4% a=(${(s:.:)s})
zsh-4.3.4% print $a[1]
5
zsh-4.3.4% print $a[3]
314

With ksh93/bash:
Code:
bash 3.2.25(1)$ s="5.2.314"
bash 3.2.25(1)$ set -- ${s//./ }
bash 3.2.25(1)$ echo $1
5
bash 3.2.25(1)$ echo $3
314

# 3  
Old 11-12-2007
More rustic, more complicated, but running anywere I think Smilie :

Code:
#!/bin/ksh

a="5.2.314"
i=1

while [ "$a" != "" ]
do
        x[$i]=$(echo $a | cut -d. -f1)
        typeset x$i=${x[$i]}
        ((i=i+1))
        a=$(echo $a | cut -s -d. -f2-)
done

echo "x1=$x1, x2=$x2, x3=$x3"

# 4  
Old 11-13-2007
#!/bin/ksh

str="5.2.314"

Var1=`echo $str | awk -F\. '{print $1}'`
Var2=`echo $str | awk -F\. '{print $2}'`
Var3=`echo $str | awk -F\. '{print $3}'`

echo "$Var1 $Var2 $Var3"



enjoy Smilie
This User Gave Thanks to lione.heart For This Post:
# 5  
Old 11-13-2007
awk

Hi,

CODE:
Code:
echo 5.2.3.314 | awk 'BEGIN{FS="."}
{
for (i=1;i<=NF;i++)
print "var"i"="$i
}'

OUTPUT:
Code:
var1=5
var2=2
var3=3
var4=314

# 6  
Old 11-13-2007
In a script (ksh):

i=1
echo 5.2.314 | sed 's/\./ /g' | while read n
do
echo "var$i = $n"
let i=i+1
done
# 7  
Old 11-14-2007
Thank you very much everybody for your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Enhance existing script: Extract Multiple variables & Input in an echo string

Hi Experts I need your help to optimize my script to execute better as I have nearly 1M records & the script is taking close to 40 minutes to execute, so would need support on a faster alternative. Input: file {"house":"1024","zip":"2345","city":"asd","country":"zzv"}... (2 Replies)
Discussion started by: nk1984
2 Replies

2. Shell Programming and Scripting

Extract a string from a file and store it in variable

Hi, I've a file ImageSizeDetails.txt with the following contents: Image Name: swncd 01.10.00.04 Created: Wed Jan 9 14:05:48 2013 Image Type: ARM Linux Multi-File Image (gzip compressed) Data Size: 7351011 Bytes = 7178.72 kB = 7.01 MB Load Address: 00008000 Entry Point: ... (6 Replies)
Discussion started by: Parameswaran
6 Replies

3. Shell Programming and Scripting

A way to store 2 random numbers from a for loop?

I have a for loop that cycles twice and generates 1 random number for each pass through. I would like to be able to store the two numbers to use later for arithmetics. Is there a way to do that? Right now I can only seem to use the last random number for anything. Thanks. (4 Replies)
Discussion started by: AxlVanDamme
4 Replies

4. Shell Programming and Scripting

store multiple variables in one go

Guys anyone know how i can store fields into multiple variables in one go? I'm wanting to grab the disk id's from format into disk1 and disk2 Below is what i want to work but i know it doesnt :- : | format | awk '/^(\ +)/ {print $2}' | read disk1 disk2 The below does work...but i don't... (5 Replies)
Discussion started by: lavascript
5 Replies

5. Shell Programming and Scripting

[Doubt] How can I store numbers less than 1? Shell variables

Hi everyone, I am having some problems with my scripts so I hope you could help me. I am trying to store the result of a division in a variable in tcshell but I have the problem that if: For example, dividing 2/100 the result is 0.02 but if I store that I have "0". How can I have 0.02... (8 Replies)
Discussion started by: Bloody
8 Replies

6. UNIX for Dummies Questions & Answers

trying to store variables in an array

im looping through an array setting three variables each time (one of the variables gives me the position in the array and is incremented each loop) im trying to then set the variables to that position in the array without much luck. any ideas? anArray=`$VAR1+$VAR2+"("$pos")"` (1 Reply)
Discussion started by: magnia
1 Replies

7. Solaris

can array store float point numbers

Hi all, I have doubt can array in a shell script can store floating point numbers. i have tired. but i unable to work it out. Please help me regarding this Thank U Naree (1 Reply)
Discussion started by: naree
1 Replies

8. UNIX for Dummies Questions & Answers

extract from string variable into new variables

I have a variable which consists of a string like this: 001 aaabc 44 a bbb12 How do I extract each substring, delimited by the spaces, into new variables - one for each substring? eg var1 will be 001, var2 will be aaabc, var3 will be 44, var4 will be a, etc? I've come up with this:... (2 Replies)
Discussion started by: Sniper Pixie
2 Replies
Login or Register to Ask a Question