×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Need to split string into 2 variables3

## Need to split string into 2 variables

(OP)
Hi folks,

I'm looking for a way to split certain strings into 2 variables. There are 4 possible types of strings. Every string begins with 1 or 2 letters followed by 1 or 2 numbers.

#### CODE

Example 1:

String = A2
Var 1  = A
Var 2  = 2

Example 2:

String = B65
Var 1  = B
Var 2  = 65

Example 3:

String = CC4
Var 1  = CC
Var 2  = 4

Example 4:

String = DD12
Var 1  = DD
Var 2  = 12

In other words: How can I automatically part the letters from the numbers and store each one in a separate variable ?

Regards,
Thomas
Replies continue below

### RE: Need to split string into 2 variables

You could use egrep I suppose

#### CODE

var1 = echo $string | egrep -o -i '[A-Z]+' var2 = echo$string | egrep -o '[0-9]+'
but clunky though...

Cheers,
Scott

### RE: Need to split string into 2 variables

#### CODE

Var1=expr "$String" : '$$[A-Z]*$$[0-9]*' Var2=expr "$String" : '[A-Z]*$$[0-9]*$$'

### RE: Need to split string into 2 variables

(OP)
Thanks a lot everyone !
Both methods are working perfectly ...

### RE: Need to split string into 2 variables

Yet another way...

#### CODE

#!/bin/ksh

STR=ABC123
VAR1=${STR%%[0-9]*} VAR2=${STR##*[a-zA-Z]}
I included the she-bang line since this is Korn shell only (I believe).

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!