Find the answer to your Linux question:
Results 1 to 6 of 6
There is a script that I am trying to write, but I can't get this RegEx working. I want the regex to find all filenames from 1.jpg to 99999.jpeg. I ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    3

    RegEx problems

    There is a script that I am trying to write, but I can't get this RegEx working. I want the regex to find all filenames from 1.jpg to 99999.jpeg. I can get it to show results if I strip out "{1,6}", "e?", and add ".*" in there, but that ends up showing more than I want it to show. Tried I think all of the the regextype variations.

    Code:
    find "./images/" -regextype "posix-extended" -iregex ".*[/][0-9]{1,6}\.jpe?g[^/]$"
    Folder contents:
    Code:
    ls -l ./images/
    total 0
    -rw-r--r-- 1 bjohnson users 0 2008-10-29 07:38 041.jpg
    -rw-r--r-- 1 bjohnson users 0 2008-10-29 10:48 31.jpg
    -rw-r--r-- 1 bjohnson users 0 2008-10-29 10:48 54364.jpg

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    I think you need to get rid of the trailing "[^/]", which is matched by any single character, except "/". I assume that there is no such character after "jpe?g".

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Posts
    3
    Oops. Now that you mention that, it all makes sense now.

    And that works. I was thinking differently when I added that. I was thinking that it would not include any folder names in the search result. Thanks.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    take note when you use find's -regex switch. Please read the find man page for some caveats.
    anyway, no need for regex
    Code:
    # ls -1
    002.jpg
    01.jpg
    5346.jpg
    test.jpg
    
    # ls -1 [0-9]*.jpg
    002.jpg
    01.jpg
    5346.jpg
    if you want to use find
    Code:
     # find . -type f -name "[0-9]*.jpg"
    ./5346.jpg
    ./002.jpg
    ./01.jpg

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    There are differences between filename expressions and regular expressions. Use the one that is least complex that filters the filenames that you need. Depending of the situation you may need even more complexity:
    Code:
    #!/bin/bash -
    
    # @(#) s1       Demonstrate find with regular expression, not shell expression.
    
    echo
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) find
    set -o nounset
    
    FILE1="1.jpg 99999.jpeg"
    FILE2="z3.jpg 3.jpg.xxx"
    FILE3="002.jpg 01.jpg 5346.jpg test.jpg 99x.jpg 98_x_.jpg 99999999.jpg"
    FILES="$FILE1 $FILE2 $FILE3"
    rm -f $FILES
    touch $FILES
    
    echo
    echo " All files:"
    ls -1
    
    echo
    echo " All .jpg files starting with decimal:"
    ls -1 [0-9]*.jpg
    
    echo
    echo " find with filename-expansion (glob) expression:"
    find . -type f -name '[0-9]*.jpg'
    
    echo
    echo " find with regular expression (on entire path -- see man page):"
    find . -type f -regex './[0-9]*.jpg'
    
    echo
    echo " find with length-limited regular expression:"
    find . -regextype posix-egrep -type f -iregex './[0-9]{1,5}.jpe?g'
    
    exit 0
    Producing:
    Code:
    $ ./s1
    
    (Versions displayed with local utility "version")
    Linux 2.6.26-1-686
    GNU bash 3.2.39
    find (GNU findutils) 4.4.0
    
     All files:
    002.jpg
    01.jpg
    1.jpg
    3.jpg.xxx
    5346.jpg
    98_x_.jpg
    99999.jpeg
    99999999.jpg
    99x.jpg
    s1
    t1
    test.jpg
    z3.jpg
    
     All .jpg files starting with decimal:
    002.jpg
    01.jpg
    1.jpg
    5346.jpg
    98_x_.jpg
    99999999.jpg
    99x.jpg
    
     find with filename-expansion (glob) expression:
    ./99x.jpg
    ./01.jpg
    ./98_x_.jpg
    ./002.jpg
    ./99999999.jpg
    ./5346.jpg
    ./1.jpg
    
     find with regular expression (on entire path -- see man page):
    ./01.jpg
    ./002.jpg
    ./99999999.jpg
    ./5346.jpg
    ./1.jpg
    
     find with length-limited regular expression:
    ./99999.jpeg
    ./01.jpg
    ./002.jpg
    ./5346.jpg
    ./1.jpg
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Just Joined!
    Join Date
    Oct 2008
    Posts
    3
    ghostdog, drl - Thanks for your input. Although I already have my solution for this particular issue, I appreciate your willingness to help.

Posting Permissions

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