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
How to sort a string with numbers ahjiefreak Shell Programming and Scripting 5 12-21-2007 10:52 AM
to find numbers in a string fongthai Shell Programming and Scripting 12 11-22-2007 04:34 PM
Extract numbers from a string and store in variables davewg Shell Programming and Scripting 6 11-14-2007 05:22 AM
perl: string comparison as numbers bishweshwar UNIX for Advanced & Expert Users 7 05-27-2007 01:54 AM
output only numbers from mixed string nortypig Shell Programming and Scripting 10 08-29-2006 11:30 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 07-18-2007
eliraza6 eliraza6 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 56
How do i get numbers from a string?

Hi...
I'm new here and i have a Q...

How do i get only the number from a string?
like from "rlvol11" i want to get 11
or from "lvol4" i want to get 4

what commands should i use at my script?

thanx 4 the help!

Eliraz.
  #2 (permalink)  
Old 07-18-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
in ksh/bash

Code:
echo $variable |  tr -d '[A-Za-z]' | read numStr
Example

Code:
echo "lvol4" |  tr -d '[A-Za-z]' | read numStr
echo $numStr
  #3 (permalink)  
Old 07-18-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
One way is

Code:
echo "rlvol11" | tr -d '[:alpha:]'
See man tr for more details.
  #4 (permalink)  
Old 07-18-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,892
With bash:

Code:
$ v="rlvol11";echo "${v//[!0-9]}"
11
  #5 (permalink)  
Old 07-18-2007
bigearsbilly bigearsbilly is offline
Registered User
  
 

Join Date: Feb 2006
Location: Southern England
Posts: 104
better:

tr -dc '[0-9]'
  #6 (permalink)  
Old 07-18-2007
eliraza6 eliraza6 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 56
hi again...

First of all thanx!
it worked but i still have a problem with my script...
i want to make a script that gets a VG name and outputs the size of it's lvols...

here is what i wrote but it doesn't work...

Code:
for x in *
    do
        if [ "echo $x" = "lvol*" ]; then
    echo $x |  tr -d '[A-Za-z]' | read lvolnum
    echo /dev/$1/lvol$lvolnum
    lvdisplay lvol${lvolnum} | grep Mbytes
        fi
done
can someone please help me?
thanx alot!
  #7 (permalink)  
Old 07-18-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by eliraza6 View Post
Code:
for x in *
    do
        if [ "echo $x" = "lvol*" ]; then
    echo $x |  tr -d '[A-Za-z]' | read lvolnum
    echo /dev/$1/lvol$lvolnum
    lvdisplay lvol${lvolnum} | grep Mbytes
        fi
done
Code:
for x in *
do
      if [ "$x" = lvol* ]; then
        echo $x |  tr -d '[A-Za-z]' | read lvolnum
        echo /dev/$1/lvol${lvolnum} --> Where did you get $1 from ?
        lvdisplay lvol${lvolnum} | grep Mbytes
      fi
done
I have a feeling it all boils down to just

Code:
for x in lvol*
do
  echo /dev/$1/$x
  lvdisplay $x | grep MBytes
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 08:36 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