Solved

Regular Expression to extract a number from a variable value

Posted on 2008-10-14
3
949 Views
Last Modified: 2008-10-27
Hi,

I am getting different set of values in a variable. These values can be adminWash4567.sh adminWash4567.txt eastLA7456.sh eastLA7456.txt westLA7825 oldSFB8523 etc. I basically am trying to extract the last 4 digits of the value which starts with "admin".
I think it should work something like this

if the value starts with "admin" save last 4 digits in a variable before the "." (as in adminWash4567.sh) and exit

it would be great if you can suggest a way to do this
0
Comment
Question by:LordSM
3 Comments
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 22715630
A generic solution is



value='adminWash4567.sh'

result=`echo "$value" | sed "s/admin.*\([0-9][0-9][0-9][0-9]\).*/\1/"`

echo $result

Open in new window

0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 100 total points
ID: 22715639
try

var=`echo  adminWash4567.sh | sed 's/[a-zA-Z\.]*//' | awk -F'.' '{ print $1 }'`
0
 

Accepted Solution

by:
LordSM earned 0 total points
ID: 22783118
Generic solution works

value='adminWash4567.sh'
result=`echo "$value" | sed "s/admin.*\([0-9][0-9][0-9][0-9]\).*/\1/"`
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now