Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2006
Registered User
 

Join Date: Nov 2006
Posts: 2
Extract digits at end of string

I have a string like xxxxxx44. What's the best way to extract the digits (one or more) in a ksh script?

Thanks
Sponsored Links
  #2 (permalink)  
Old 11-20-2006
Registered User
 

Join Date: Oct 2005
Posts: 67
elaborate your question.

can u elaborate your question /provide some sample input.
what do u want to do exactly?
  #3 (permalink)  
Old 11-20-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,431
Try...
Code:

var1="xxxxxx44"
var2=$(expr $var1 : "[^0-9]*\([0-9]*\)")

  #4 (permalink)  
Old 11-20-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,434
Extraction of the trailing digits :

Code:

var=xxxxx44
digits=${var#${var%%*([0-9])}}


Jean-Pierre
  #5 (permalink)  
Old 11-20-2006
Technorati Master
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 3,006
how about this,


Code:

echo "abcde444" | sed 's/[a-z]//g'

  #6 (permalink)  
Old 11-20-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,434
Ygor and matrixmadhan, your solutions works fine, but only if the trailing digits are prefixed by non digit characters.

For example :

Code:

var="xxx1xxx44"
expr $var1 : "[^0-9]*\([0-9]*\)"   # => Gives 1
echo $var1 | sed 's/[a-z]//g'        # => Gives : 144


Jean-Pierre.
  #7 (permalink)  
Old 11-20-2006
Registered User
 

Join Date: Nov 2006
Posts: 2
Quote:
Originally Posted by matrixmadhan
how about this,


Code:

echo "abcde444" | sed 's/[a-z]//g'


Thanks - that's just what I was trying to do.

Thanks all for your replies
Sponsored Links
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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Ksh Checking if string has 2 characters and does not contain digits? developncode UNIX for Dummies Questions & Answers 1 04-08-2008 04:19 PM
Extract string between <> rakeshou Shell Programming and Scripting 14 11-21-2007 01:10 PM
to check if a string has only digits lakshmikanth UNIX for Dummies Questions & Answers 3 11-20-2006 09:45 AM
Extract String sehgalniraj UNIX for Dummies Questions & Answers 1 09-25-2006 01:35 PM
How to extract a portion of a string from the whole string ds_sastry UNIX for Dummies Questions & Answers 2 09-29-2001 11:40 AM



All times are GMT -4. The time now is 10:18 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-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0