The UNIX and Linux Forums  

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
extract numbers from a word systemali Shell Programming and Scripting 14 03-30-2009 04:47 AM
can array store float point numbers naree SUN Solaris 1 03-05-2008 06:59 AM
How to store the data retrived by a select query into variables? jisha Shell Programming and Scripting 12 01-17-2008 11:45 PM
extract from string variable into new variables Sniper Pixie UNIX for Dummies Questions & Answers 2 03-03-2006 10:49 AM

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-12-2007
davewg davewg is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 22
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 (permalink)  
Old 11-12-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
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 (permalink)  
Old 11-12-2007
V3l0 V3l0 is offline
Registered User
  
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
More rustic, more complicated, but running anywere I think :


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 (permalink)  
Old 11-13-2007
lione.heart lione.heart is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 13
#!/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
  #5 (permalink)  
Old 11-13-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,097
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 (permalink)  
Old 11-13-2007
prowla prowla is offline
Read Only
  
 

Join Date: Nov 2007
Posts: 165
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
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 04:19 AM.


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