The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
ksh - test if string contains alphanumeric... tugger Shell Programming and Scripting 3 10-16-2007 04:23 AM
AlphaNumeric String Operations lakshmikanth UNIX for Dummies Questions & Answers 3 01-05-2007 06:55 AM
Wanted to eliminate numeric part from a filename Sona UNIX for Dummies Questions & Answers 8 07-20-2006 02:49 PM
Convert string to numeric kflee2000 Shell Programming and Scripting 3 11-19-2003 11:21 PM
matching alphanumeric string sskb Shell Programming and Scripting 4 12-12-2001 10:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-30-2007
ozgurgul ozgurgul is offline
Registered User
  
 

Join Date: Sep 2006
Location: Prague, CZ
Posts: 3
With Regex Spliting the string into Alphanumeric and Numeric part

Hi there

With shell script I'm trying to split the string into two parts. One is alphanumeric part, the other one is a numeric part.
Code:
dummy_postcode_1 = 'SL1' 
--> res_alpha = 'SL' and  res_numeric = '1' 
dummy_postcode_2 = 'S053' 
--> res_alpha = 'S' and  res_numeric = '053' 
dummy_postcode_3 = 'SO5R3' 
--> res_alpha = 'SO5R' and res_numeric = '3' 
dummy_postcode_4 = 'SO53R' 
--> res_alpha = 'SO53R' and res_numeric = ''
dummy_postcode_5 = '783652' 
--> res_alpha = '' and res_numeric = '783652' 
The logic of splitting these postcode strings is the rightmost alphanum character it seems and I could do that with the character checking but it seems to me this type of coding is a bit silly to do so.

Could you advise me how i can split them into two parts by using regex?

Thanks in advance,

Ozgur
  #2 (permalink)  
Old 06-30-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
With ksh and bash (with extglog option enabled) you can do :

Code:
echo "SL1
S053
SO5R3
SO5R
783652" |
while read var
do
   res_alpha=${var%%*([0-9])}
   res_num=${var#$tes_alpha}
   echo "$var = '$res_alpha' + '$res_num'"
done
Output:
Code:
SL1 = 'SL' + '1'
S053 = 'S' + '053'
SO5R3 = 'SO5R' + '3'
SO5R = 'SO5R' + ''
783652 = '' + '783652'
With all shells you can also do the work with expr :
Code:
echo "SL1
S053
SO5R3
SO5R
783652" |
while read var
do
   res_alpha=`expr "$var" : '\(.*[^0-9]\+\)[0-9]*'`
   res_num=`expr "$var" : "${res_alpha}\(.*\)"`
   echo "$var = '$res_alpha' + '$res_num'"
done
Output:
Code:
SL1 = 'SL' + '1'
S053 = 'S' + '053'
SO5R3 = 'SO5R' + '3'
SO5R = 'SO5R' + ''
783652 = '' + '783652'

Last edited by aigles; 06-30-2007 at 10:06 AM.. Reason: add of expr solution
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 On




All times are GMT -4. The time now is 04:55 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