Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
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 ...
  1. #1
    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

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    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.

  3. #3
    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

  4. #4
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    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.

  5. #5
    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.

  6. #6
    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.

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    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]*"

  8. #8
    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.

  9. #9
    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.

  10. #10
    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 .*

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...