tags:

views:

37

answers:

2

Hi all

I have the following names of files: When x is a number (x could be number between 0 to 100)

fileX
fileXblabla
fileX.X
fileX.Xblabla

For example

fileX --> file1
fileXblabla --> file1blabla
fileX.X --> file5.3
fileX.Xblabla --> file2.9blabla

my question is how to match each one of the following kind of files under directory (by ls and Regular Expressions or something else)

For example

 fileX

Under directory we want to get all files like: file1 or file2 or file3 …..file100

 fileXblabla

Under directory we want to get all files like: file1bla or file2bla or file3bla …..file100bla

 fileX.X

Under directory we want to get all files like: file1.1 or file2.1 or file3.1 …..file100.100

 fileX.Xblabla

Under directory we want to get all files like: file1.1bla or file2.1bla or file3.1bla …..file100.100bla

A: 

The following REGEX will match all 4 patterns:

file(\d{1,3})(\w*)(?:\.(\d{0,3})(\w*))?

The X in fileX will be in back reference 1
The blabla in fileXblabla will be in back reference 2
The Y in fileX.Y will be in back reference 3
The blabla in fileX.Yblabla be in back reference 4.

If you need any more help just raise add a comment.

Jonathan Stanton
This will match file number from 0 thru to 999. If you want to limit it to 0 to 100 then replace the (\d{1,3}) with shinkou's (100|[1-9]\d|\d) modification.
Jonathan Stanton
how to write the grep with file(\d{1,3})(\w*)(?:\.(\d{0,3})(\w*))?
yael
ls | grep file((\d{1,3})(\w*)(?:\.(\d{0,3})(\w*))? it dosent work
yael
ls | grep ^file(100|[1-9]\d|\d)(\.(100|[1-9]\d|\d))?(bla)*$-bash: syntax error near unexpected token `('any help??
yael
ls | grep "^file\([0-9]\{1,3\}\)\([[:alnum:]]*\)\(\.[0-9]\{1,3\}[[:alnum:]_]*\)$" This should work from a shell prompt (This is POSIX complient)
Jonathan Stanton
Yeal does the above solve you issue?
Jonathan Stanton
A: 

Try this:

^file(100|[1-9]\d|\d)(\.(100|[1-9]\d|\d))?(bla)*$
shinkou
if you can please give example how to exe this syntax wilh ls command
yael