Removing Letters from Integer String | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Removing Letters from Integer String

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-12-2003
hodges hodges is offline
Registered User
 
Join Date: May 2003
Last Activity: 12 June 2003, 10:12 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Removing Letters from Integer String

Hi all,

I have a variable, on some machines it is '1024', which is fine, but on others it is '1024Mb' etc. I need this variable to simply be '1024', does anyone know how I could ensure this is always the case? Perhaps a command to remove any letters/characters that aren't integers if there is one?

Thanks.
Sponsored Links
    #2  
Old 06-12-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
 
Join Date: Aug 2002
Last Activity: 3 October 2004, 6:07 PM EDT
Location: Cleveland, OH
Posts: 804
Thanks: 0
Thanked 6 Times in 5 Posts
Well, you could use
Code:
echo "1024MB" | awk '{print substr($1,0,4)}' # print first four numbers / chars
echo "1024MB" | sed 's/[A-Za-z]*//g'         # removes all letters
x=${x%%[A-Za-z]*}           # remove everything after (and including) any chars


Last edited by oombera; 06-12-2003 at 12:27 PM..
Sponsored Links
    #3  
Old 06-12-2003
criglerj's Avatar
criglerj criglerj is offline
Registered User
 
Join Date: May 2002
Last Activity: 7 September 2008, 10:24 PM EDT
Location: Atlanta
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Assuming sh, ksh, zsh (, bash?):

Code:
y=1024
z=1024Mb
sizey=`expr "$y" : '\([0-9][0-9]*\)'`
sizez=`expr "$z" : '\([0-9][0-9]*\)'`

$sizey and $sizez will now both be 1024.

Note that oombera's third solution depends on ksh.
    #4  
Old 06-13-2003
yeheyaansari yeheyaansari is offline
Registered User
 
Join Date: Feb 2002
Last Activity: 26 July 2003, 3:40 AM EDT
Location: India
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Try this:
echo 1245MB |tr -d [A-Z][a-z]

-Yeheya
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing letters from an array into a string for string comparison lotsofideas Homework & Coursework Questions 5 02-29-2012 05:28 AM
how to compare string integer with an integer? h0ujun Shell Programming and Scripting 4 02-09-2012 11:05 AM
integer to string f_o_555 UNIX for Dummies Questions & Answers 4 12-16-2009 06:59 AM
Converting integer to String ROOZ Shell Programming and Scripting 1 06-05-2008 02:38 PM
Integer to String psilva Programming 2 08-17-2001 01:14 PM



All times are GMT -4. The time now is 01:27 PM.