1752749 Members
4867 Online
108789 Solutions
New Discussion юеВ

Problem with wilcards

 
SOLVED
Go to solution
R.O.
Esteemed Contributor

Problem with wilcards

Hi,

I am trying to mach files named like this:

file1.200705152407

where the digits 200705 are day, month and year.
I want to list all the files that matches the pattern:

file1.(any day)MMYY(any five numbers)

How can I do it? I tried with *, ?, but i does not work.

Regards,
"When you look into an abyss, the abyss also looks into you"
11 REPLIES 11
R.O.
Esteemed Contributor

Re: Problem with wilcards

Sorry:

file1.(any day)MMYY(any SIX numbers)

Regards,
"When you look into an abyss, the abyss also looks into you"
Simon Hargrave
Honored Contributor

Re: Problem with wilcards

Why does ? not work? I tested it with some test files: -

# ls -1
file1.010203123451
file1.020203123441
file1.020203123451
file1.020303123451
# ls file1.??0203??????
file1.010203123451
file1.020203123441
file1.020203123451

This displays all the files from 0203. Is this not what you want?

A. Clay Stephenson
Acclaimed Contributor

Re: Problem with wilcards

You can use "[0-9]" to represent any single digit; multiple "[0-9]"'s can be strung together.

Often it's actually easier to simply punt and do an ls (of everything, or ls file1*) and pipe to enhanced grep for more serious regular expression evaluation:

ls | grep -E -e '^file1\.[0-9]{12}$'

which would list file1.followed by exactly 12 digits which seems to be a simpler statement of your problem.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Problem with wilcards

Clay is correct. You can use the wildcard "*" character to specify the preceding range of character(s) you want to match. The following command will list all the files:

# ls -1 file1.[0-9]*

cheers!
R.O.
Esteemed Contributor

Re: Problem with wilcards

Hi,

Thank you for your responses!!! It happens in a script. If I put in the script:

file1.*${MONTH}${YEAR}*

... it works; but if I put

file1.??${MONTH}${YEAR}??????

....it does not works:

file1.??0605?????? not found

What is wrong?

Regards,
"When you look into an abyss, the abyss also looks into you"
V. Nyga
Honored Contributor

Re: Problem with wilcards

Hi,

you said:
file1.(any day)MMYY(any five numbers)

but you made six '?' at the end:
file1.??${MONTH}${YEAR}??????

That can't work!
You have to make exactly as much '?' as numbers you search.
So with 5 '?' it should work.

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
V. Nyga
Honored Contributor

Re: Problem with wilcards

Sorry - you corrected your number in your first answer.

Can you show us your list command?

V.
*** Say 'Thanks' with Kudos ***
Muthukumar_5
Honored Contributor

Re: Problem with wilcards

IF your file is like,

file1.200705 then,

#!/bin/ksh
#list.ksh

# User Input
Month="" # two digit example 09
Year="" # two digit 05
LS=`which ls`
$LS file1.??${Month}${Year}?????

exit 0
# end

chmod 755 list.ksh
./list.ksh

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor
Solution

Re: Problem with wilcards

To elloborate more on this,

ls command will use ? * as pattern matching not with regular expression.

ls file1.??<2digit><2digit>??????

It will try to find files with pattern match of file1.two digits <2 digits><2 digits> <6 digits> else it will try to match for,

ls file1.??0205??????? named file which is created as touch file1.??0205??????

hth.
Easy to suggest when don't know about the problem!