mardi 30 décembre 2014

how to find files based on filename range?


I have some files with numeric names: 2341a.po, 4567211someword.po, 0012.po, etc. I would like to find a set of files based on the numeric range. such as [126 - 363].


Normally I use regular expression with find. Any numeric range [N, M] can be composed with two regular expressions larger than N and less than M.


The larger than N:


If N = vxyz, then I first do a match for all value > V000, (V=v+1) with [V-9]\d{3,}


Then match vX00, X=x+1, with v[X-9]\d\d


Then match vxY0, Y=y+1, with vx[Y-9]\d


And last vxy[z-9]


Example:


To match number>=234, I use:



`^(0*([3-9]\d{2,}|2[4-9]\d|23[4-9]))`


The smaller than M:


Based on similar logic we will have:



^(0*(vxy[0-z]|vx[0-Y]\d|v[0-X]\d\d|[1-V]\d\d|\d{1,3}))[^0-9]


With Y=y-1,X=x-1,V=v-1


For example, the follow command will find any file between [253, 326]:



find . -maxdepth 1 -type f -regextype posix-extended -iregex '^\./0*([3-9][0-9]{2,}|2[6-9][0-9]{2,}|25[3-9]).*' -iregex '^\./0*(32[0-6]|3[0-1][0-9]|[0-2][0-9][0-9]|[0-9]{1,2})[^0-9].*'


However, this method is too annoying to handle long numbers. Is there any better and easier way to do this?



Aucun commentaire:

Enregistrer un commentaire