cancel
Showing results for 
Search instead for 
Did you mean: 

Regex

SOLVED
Go to solution
Mike Boswell_1
Frequent Advisor

Regex

/usr/local/tomcat_staged_war_files/web_serv_8080/TestResults_WS_08120309.war

Given the above string, how do I substitute the _08120309 resulting in:
/usr/local/tomcat_staged_war_files/web_serv_8080/TestResults_WS.war

Also, How would I use 'find' to only find a file that contains this (_08120309) convention? This is a date string and is variable but alwas a '_' followed by 8 digits.

Thanks !

Mike

 

 

P.S. this thread has been moved from Insight Remote Support>general to Linux > sysadmin - HP Forums Moderator

7 REPLIES
Mike Boswell_1
Frequent Advisor

Re: Regex

Maybe the wrong forum. I am scripting in bash on redhat.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Regex

Hi Mike:

F="/usr/local/tomcat_staged_war_files/web_serv_8080/TestResults_WS_08120309.war"

# echo ${F}|perl -ple 's/_08120309//'
/usr/local/tomcat_staged_war_files/web_serv_8080/TestResults_WS.war

# cd /path && perl -MFile::Find -le 'find(sub{print if -f $_ && m/_\d{8}/},".")'

...The '\d' represents a digit and the '{8}' says eight of them, please.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Regex

Hi (again) Mike:

> Maybe the wrong forum. I am scripting in bash on redhat.

Well, Perl is universal :-)

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Regex

You can also use sed to change the string:
echo $OLD | sed -e 's/_[0-9]\{8\}//'

# Assuming a "." after the numbers
find path -name "*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*"

Mike Boswell_1
Frequent Advisor

Re: Regex

James, Thanks for your quick response. I dont speak perl, so your solutions would be plagiarism on my part. One day I will figure it out. I do have a book but have never been able to get to far into it.

Dennis, Sed is more my style, in fact I was trying to make that exact command work. Looks like all I needed were the quotes. What is the -e for? It works w/ or wo/ it. So, find and sed dont have the same regex capabilities, eh?

Thanks Guys !
Mike Boswell_1
Frequent Advisor

Re: Regex

Thanks again.
Dennis Handly
Acclaimed Contributor

Re: Regex

>What is the -e for? It works w/ or wo/ it.

Using -e allows you to have multiple scripts, same as grep. It is a good habit to get into.

>find and sed don't have the same regex capabilities, eh?

No. See regexp(5):
http://docs.hp.com/en/B2355-60130/regexp.5.html

sed/grep takes Basic Regular Expressions
find/ls/shells take Pattern Matching Notation
awk/egrep/grep -e take Extended Regular Expressions
perl takes the kitchen sink. :-)