Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract certain characters 2

Status
Not open for further replies.

macca007

Programmer
May 1, 2004
86
GB
Hi all,

I would like to extract certain numbers and characters from string of letters and numbers e.g

3452_dx2443_joeBloggs

what i would like to extract is the

dx2443 which is the example patient hospital number it always starts with 2 letters then followed by numbers.

i only know very simple unix scripting, so some sample unix script to get me going will be much helpful.

many thanks
 
var="3452_dx2443_joeBloggs"
If always surrounded by underscores:
# One way in all bourne compatible shells
[tt]hospno=`expr "$var" : '[^_]*_\([^_]*\)_.*'`[/tt]
# Another way in ksh-like shells
[tt]hospno=${s#*_}; hospno="${t%_*}"[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Oops, sorry for the typo:
# Another way in ksh-like shells
s=${var#*_}; hospno="${s%_*}"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
once again thanks for your reply.

your script will work for certain scenarios. However sometimes the users who write the filename may change the format of it.
e.g
the actual format(98% of times is this)

2345_dx3945_29_10_50_Blogs_joe

the other 2% may write it like this
dx3956_Blogs_joe
32243__dx3945_Blogs_joe
blogs_joe_dx3984

Those 2% always make my life slighlty harder [sad](but u always consider user error/input)

Anyway back to the script is it possible some how 2 look for 2 letters then 4 numbers followed by this. (This they cannot get wrong-don't ask me why but no one has done it yet! But am waiting for it 2 happen)

cheers
 
You may try this:
[tt]hospno=`expr "$var" : '.*\([a-z][a-z][0-9][0-9][0-9][0-9]\).*'`[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
echo "blogs_joe_dx3984"|awk '
BEGIN{RS="_"}
/^[a-z][a-z][0-9]+\n?$/{sub(/\n/,"");print}'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top