Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Newbie > Script Problem

Forgot Password?
 Linux Newbie   If you're new to the wonderful world of Linux, start here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 06-15-2009   #1 (permalink)
Just Joined!
 
Join Date: Jun 2009
Posts: 3
Unhappy Script Problem

I hope you can put me in the correct path as I am having a bit of
difficulty writing a script to search for directories that begin with
2 characters and 3 numbers and does not mater about the rest of the
name of the directory for example:

GS001_Anything_test
TS636_Test

Ideally I would like to pattern match [any 2 characters] and
[3 numbers]and the rest as a *

I hope my problem that makes sense

Please Help
helpneeded is offline  


Reply With Quote
Old 06-15-2009   #2 (permalink)
Linux Enthusiast
 
Join Date: Mar 2008
Posts: 601
Hi,

you will have to learn regular expressions for this.
With these, you can write critera like
[:alpha:] for characters, [:digit:] for numbers.
Regular expression - Wikipedia, the free encyclopedia
__________________
Debian GNU/Linux -- You know you want it.
GNU-Fan is offline   Reply With Quote
Old 06-15-2009   #3 (permalink)
Just Joined!
 
Join Date: Jun 2009
Posts: 3
Scripts problem

do you mean like the following

find /usr -name [abcdefghijklmnopqrstuvwxyz]+[1234567890]+*

how do you fix the find and make it search from the left and only upto 6 characters and numbers until it reaches _*

then display results
helpneeded is offline   Reply With Quote
Old 06-15-2009   #4 (permalink)
Linux Enthusiast
 
Join Date: Mar 2008
Posts: 601
Quote:
Originally Posted by helpneeded View Post
do you mean like the following

find /usr -name [abcdefghijklmnopqrstuvwxyz]+[1234567890]+*

how do you fix the find and make it search from the left and only upto 6 characters and numbers until it reaches _*

then display results
No, I mean like
find /usr -regex PATTERN
where PATTERN is a code sequence you have to develop based on the link above.
__________________
Debian GNU/Linux -- You know you want it.
GNU-Fan is offline   Reply With Quote
Old 06-23-2009   #5 (permalink)
Banned
 
Join Date: Jun 2009
Posts: 68
The regular expression I believe you're talking about would be:
Code:
/([A-Z]|[a-z]){2}\d{3}.*/
to match. Printing it's dependent on what you use to match and print. I'm not so sure.
nopycckn is offline   Reply With Quote
Old 06-23-2009   #6 (permalink)
Banned
 
Join Date: Jun 2009
Posts: 68
[abcdefghijklmnopqrstuvwxyz]+[1234567890]+*
is a regular expression matching:
at least one lowercase character followed by at least one digit (an asterisk would mean any number of times, but I believe the syntax to be wrong (just [0-9]+* alone is like saying at least one digit any number of times). It will complain and not work correctly using PERL, written this way - with a message of nested quantifiers in regex.)
It can be written simply as /[a-z]+\d+/, without the asterisk, but it's not the right regular expression for the job.
nopycckn is offline   Reply With Quote
Old 06-27-2009   #7 (permalink)
Linux User
 
Join Date: Aug 2006
Posts: 434
Quote:
Originally Posted by helpneeded View Post
I hope you can put me in the correct path as I am having a bit of
difficulty writing a script to search for directories that begin with
2 characters and 3 numbers and does not mater about the rest of the
name of the directory for example:

GS001_Anything_test
TS636_Test

Ideally I would like to pattern match [any 2 characters] and
[3 numbers]and the rest as a *

I hope my problem that makes sense

Please Help
Code:
find /path -type d -name "[a-zA-Z][a-zA-Z][0-9][0-9][0-9]*"
ghostdog74 is offline   Reply With Quote
Old 06-28-2009   #8 (permalink)
Banned
 
Join Date: Jun 2009
Posts: 68
I used a regular expression like you would use in PERL, but I know there are variations with find, sed, etc. Not trying to sound like an expert, but I didn't realize that there was quite so much difference between PERL regular expressions and these, also ... I thought the {2} {3} would be pretty much universal. I kind of rushed into it, I took for granted there were even more similarities. Apologies, won't happen again.
nopycckn is offline   Reply With Quote
Old 07-01-2009   #9 (permalink)
Just Joined!
 
Join Date: Jun 2009
Posts: 3
thank you all and to "ghostdog74" much appreciated.

find /path -type d -name "[a-zA-Z][a-zA-Z][0-9][0-9][0-9]*"

What do I need to do to display any directory found inside a parent directory based on find patern matching.

For example

[parent Directory1]
GS001_Anything_test
TS636_Test
[parent Directory2]
GS001_Anything_test
TS637_Test
[parent Directory3]
TT001_Anything_test
TB636_Test

Thanks for all your advice and help.
helpneeded is offline   Reply With Quote
Old 07-02-2009   #10 (permalink)
Banned
 
Join Date: Jun 2009
Posts: 68
I am very sure that the last asterisk is only applying to the last [0-9]. It's saying any amount (even 0) of [0-9]. To say anything after the last [0-9] would be .*
nopycckn is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 09:31 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2