Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: 'find' regular expression

  1. #1
    Join Date
    Jun 2009
    Beans
    470
    Distro
    Ubuntu 8.04 Hardy Heron

    'find' regular expression

    Dear all,

    I have the find below:
    Code:
    find . -regextype posix-egrep -iname "*G[0-6]0*[1-4]*REV[1-9]*.pdf"
    which returns files of certain revision number.

    But I need the latest revision number in every directory and
    this regular expression does not work as expected
    Code:
    *G[0-6]0*[1-4]*REV(9|8|7|6|5|4|3|2|1)*.pdf
    The number trick should return the highest number without sort
    but it does not work.

    What do you think ?

  2. #2
    Join Date
    Sep 2009
    Location
    Freiburg/Germany
    Beans
    1,112
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: find regular expression help

    You can't get only the highest number without comparing the numbers, and the regex can't do that as it only tries to match one filename at a time.
    ClassicMenu Indicator - classic GNOME menu for Unity
    Unsettings - configuration program for the Unity
    Privacy Indicator - easily switch privacy settings in Unity
    Arronax - create and modify app starters

  3. #3
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: find regular expression help

    It might help to post a few examples of the file names to be sorted.

    It's generally frowned upon to use "ls" in a script, but the following might work:

    Code:
    ls -v *.pdf | tail -n 1
    Last edited by Lars Noodén; November 8th, 2012 at 04:47 PM. Reason: fixed typo

  4. #4
    Join Date
    Jun 2009
    Beans
    470
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: find regular expression help

    In every directory there should be files like this:
    somecharacters-G101-REV2-sometext.pdf
    somecharacters-G101-REV4-sometext.pdf

    and I need the highest REV4 one from that directory.

    Possible with find or grep ?

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: find regular expression help

    no. Regexes don't compare anything, they find matches. You need some kind of sort to define X>Y relation between returned results.
    Last edited by Vaphell; November 8th, 2012 at 01:16 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    Join Date
    Jun 2009
    Beans
    470
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: find regular expression help

    Quote Originally Posted by Vaphell View Post
    no. Regexes don't compare anything, they find matches. You need some kind of sort to define X>Y relation between returned results.
    I know, but I tried the REV(9|8|7|6|5|4|3|2|1).

    But I think find operates on individual files and the above regex
    can't work.

    It seems that I have to use a real language for this.

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: find regular expression help

    but it didn't make any sense here
    it was equivalent of [1-9] either way

    Code:
    some-junk: does it match REV[1-9]? NO   
    file-REV3: does it match REV[1-9]? YES 
    file-REV4: does it match REV[1-9]? YES
    file-REV8: does it match REV[1-9]? YES
    names that got YES will be included in the results. That's all.

    It seems that I have to use a real language for this.
    what do you mean by real language? What's wrong with sorting and picking the first/last element? sort lets you define arbitrary sorting order.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    sort --key

    Quote Originally Posted by Vaphell View Post
    sort lets you define arbitrary sorting order.
    sort -k can do a lot, but the manual page is not very clear at all.

  9. #9
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: find regular expression help

    What about something like this (assuming your name example above is the way you've really named your files):

    Code:
    $ ls
    somechars-G101-REV1-sometext.pdf  somechars-G101-REV3-sometext.pdf  somechars-G101-REV5-sometext.pdf
    somechars-G101-REV2-sometext.pdf  somechars-G101-REV4-sometext.pdf  somechars-G101-REV6-sometext.pdf
    $ find . -type f -iname "*REV*" -exec ls {} \; | sort -t "-" -rV -k3
    ./somechars-G101-REV6-sometext.pdf
    ./somechars-G101-REV5-sometext.pdf
    ./somechars-G101-REV4-sometext.pdf
    ./somechars-G101-REV3-sometext.pdf
    ./somechars-G101-REV2-sometext.pdf
    ./somechars-G101-REV1-sometext.pdf

  10. #10
    Join Date
    Jun 2009
    Beans
    470
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: find regular expression help

    Quote Originally Posted by drmrgd View Post
    What about something like this (assuming your name example above is the way you've really named your files):

    Code:
    $ ls
    somechars-G101-REV1-sometext.pdf  somechars-G101-REV3-sometext.pdf  somechars-G101-REV5-sometext.pdf
    somechars-G101-REV2-sometext.pdf  somechars-G101-REV4-sometext.pdf  somechars-G101-REV6-sometext.pdf
    $ find . -type f -iname "*REV*" -exec ls {} \; | sort -t "-" -rV -k3
    ./somechars-G101-REV6-sometext.pdf
    ./somechars-G101-REV5-sometext.pdf
    ./somechars-G101-REV4-sometext.pdf
    ./somechars-G101-REV3-sometext.pdf
    ./somechars-G101-REV2-sometext.pdf
    ./somechars-G101-REV1-sometext.pdf
    Interesting but does not work because there might be dashes before. But I know that its the last dash the one we need.

    Can anything be improved upon that ?

    File naming syntax:
    Code:
    *G[0-6]0*[1-4]*REV[1-9]*.pdf
    Last edited by dragos2; November 8th, 2012 at 03:09 PM.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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