Solved

Regular Expression to extract a number from a variable value

Posted on 2008-10-14
3
986 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…

729 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