You are not logged in.

#1 2011-02-04 00:48:11

drm00
Member
Registered: 2010-06-01
Posts: 38

Regular expressions: find files with exactly 'n' digits in a row

Hi there,

I want to filter files that contain only a fixed number of digits, but not more (at least not in after the digits).

For example, I have

01.mp3
02.mp3
test10.txt
test000110101010.txt
04.flac

and for n=2 I want to get all files except 'test000110101010.txt'.

The following is not working, and I'm a total newb regarding regular expressions sad

ls -l | grep '^-' | awk '{print $9}' | grep '([0-9]\{2\})[^0-9]\{2\}'

Thanks for help.

Regards,
drm

Offline

#2 2011-02-04 00:57:46

toofishes
Developer
From: Chicago, IL
Registered: 2006-06-06
Posts: 602
Website

Re: Regular expressions: find files with exactly 'n' digits in a row

I have no idea at all why you are doing `ls -l` or the `awk`...

$ ls *.txt
003.txt  01.txt  02.txt  test04.txt  test050test.txt

$ ls *.txt | grep -E '(^|[^0-9]+)[0-9]{2}([^0-9]+|$)'
01.txt
02.txt
test04.txt

Offline

#3 2011-02-04 01:08:03

drm00
Member
Registered: 2010-06-01
Posts: 38

Re: Regular expressions: find files with exactly 'n' digits in a row

Thanks!! ls -l and such was just stupid big_smile

Offline

#4 2011-02-04 07:41:22

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: Regular expressions: find files with exactly 'n' digits in a row

You might want to make your life easier using find -regex in the future...

Offline

#5 2011-02-04 09:52:22

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Regular expressions: find files with exactly 'n' digits in a row

How about this, a bit harder with "edge detection" though:

shopt -s nullglob
ls -d1 [0-9][0-9] [0-9][0-9][^0-9]* *[^0-9][0-9][0-9] *[^0-9][0-9][0-9][^0-9]*

edit: added [0-9][0-9]

Last edited by Procyon (2011-02-04 13:44:22)

Offline

#6 2011-02-04 13:35:35

drm00
Member
Registered: 2010-06-01
Posts: 38

Re: Regular expressions: find files with exactly 'n' digits in a row

Thanks!

I wrote a python script to scan e.g. a music folder for missing files and needed to extract the file numbers from the files to get the "highest" number.

You can get it from here: http://pastebin.com/Sg9yDHiw (Python3, expires in 1 month)

Regards,
drm

Edit: found a bug

Last edited by drm00 (2011-02-04 13:57:43)

Offline

Board footer

Powered by FluxBB