?
Solved

Regular Expression to extract a number from a variable value

Posted on 2008-10-14
3
Medium Priority
?
991 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 800 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

752 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