Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X Find in bash with regex digits \d not producing expected results

I'm using the following regex find command in OS X terminal to find a whole load of files that have 8 digit file names followed by either a .jpg, .gif, .png or .eps extension. The following produces no results even though I've told OS X/BSD find to use modern regex

find -E ./ -iregex '\d{8}'

Using http://rubular.com/ (http://rubular.com/r/YMz3J8Qlgh) shows that the regex pattern produces the expected results and OS X produces the results when typing

find . -iname '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*'

But this seems a little long winded.

like image 660
juliushibert Avatar asked Mar 23 '12 17:03

juliushibert


People also ask

What does \d do in regex?

In regex, the uppercase metacharacter denotes the inverse of the lowercase counterpart, for example, \w for word character and \W for non-word character; \d for digit and \D or non-digit.

How do I check if a string matches in regex bash?

You can use the test construct, [[ ]] , along with the regular expression match operator, =~ , to check if a string matches a regex pattern (documentation). where commands after && are executed if the test is successful, and commands after || are executed if the test is unsuccessful.

What regex is used in bash?

Since version 3 (circa 2004), bash has a built-in regular expression comparison operator, represented by =~. A lot of scripting tricks that use grep or sed can now be handled by bash expressions and the bash expressions might just give you scripts that are easier to read and maintain.

Does bash have regex?

Regex is a very powerful tool that is available at our disposal & the best thing about using regex is that they can be used in almost every computer language. So if you are Bash Scripting or creating a Python program, we can use regex or we can also write a single line search query.


2 Answers

These commands works on OSX

find -E . -iregex '.*/[0-9]{8}\.(jpg|png|eps|gif)'

this command matches 12345678.jpg , not 123456789.jpg


find -E . -iregex '.*/[0-9]{8,}\.(jpg|png|eps|gif)'

this command matches 12345678.jpg and 123456789.jpg


.*/ 

equal the folder path or the subFolder path

like image 105
jackjr300 Avatar answered Nov 15 '22 15:11

jackjr300


With all your answers, i was finally able to use OSX find (10.8.1) with regex. For giving back, here are my findings: We use custom strings to identify clips, the pattern goes like this: "YYMMDDabc##abc*.ext": Year/Month/Day/3chars/2digits/3chars/whatever/ext

find -E /path/to/folder -type f -regex '^/.*/[0-9]{6}[A-Za-z]{3}[0-9]{2}[A-Za-z0-9]{3}\.*.*\.(ext)$'

The initial ^ makes sure the pattern is at the beginning of the search, [0-9]{6} searches for a 6 digit string, \d does'nt work. \D doesn't work for letters, A-Za-z does. The $ in the end makes sure the last search is the end of the string.

After reading Apples manpage about find and re_format i was completely off track regarding escaping characters.

like image 38
ugn Avatar answered Nov 15 '22 16:11

ugn