Solved

Regular Expression to extract a number from a variable value

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSH commands for Nas4free 21 427
Unix / Linux grid computing 5 157
Doing a DR (Disaster Recovery) Test on Red Hat Linux Servers. 2 116
remove one line from a file in solaris 8 29
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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.

821 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