Operating System - HP-UX
1832535 Members
7837 Online
110043 Solutions
New Discussion

Re: sh pattern matching problems

 
SOLVED
Go to solution
marc seguin
Regular Advisor

sh pattern matching problems

I don't understand the different results of my 'ls' and 'find' commands when using pattern matching.

Here are my files :
la1111.081.Z
la11_22.081.Z
la2222.025
labcd

I want to match files like these :
# ls la+([0-9]).081*

But I don't manage to use this pattern matching
inside a 'find' command.
# find . -name "la+([0-9]).081*"
doesn't work.

Instead,
# find . -name "la[0-9]*"
works, but this is not what i want.

Could somebody help me ?
Thanks
18 REPLIES 18
Steve Steel
Honored Contributor

Re: sh pattern matching problems

Hi


try

find la+([0-9]).081*



Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Carlos Fernandez Riera
Honored Contributor

Re: sh pattern matching problems

I think this should work
find . -name "la*[0-9].081*"

I am not a cracker in regular expresion, but you can read man 5 regexp ( 13 pages of .,+^[ and many others special chars.. ). Sure you will find the reason.
unsupported
Helen French
Honored Contributor

Re: sh pattern matching problems

Hi,

Try this:

# find . -name "la*[0-9]*081*"

Check the man pages of find and regexp.

HTH,
Shiju
Life is a promise, fulfill it!
marc seguin
Regular Advisor

Re: sh pattern matching problems

Steve, your command works but it performs substitution before really executing the 'find' command (as in my 'ls' example). So it doesn't perform a recursive find in subdirectories.

Carlos and Shizu, your pattern matches too many files (the two listed first in my example, and i want only the first one).

I've read man pages. The problem is not "what would be the pattern to match my file ?" because I see it working with the 'ls' command.
But "How to use it with the 'find' command ?"

thanks anyway
Helen French
Honored Contributor

Re: sh pattern matching problems

Hi,

Try this:

# find . -name la+([0-9]).081* -depth

This will check the subdirectories too.

HTH,
Shiju
Life is a promise, fulfill it!
harry d brown jr
Honored Contributor

Re: sh pattern matching problems



ls | grep "^la\([0-9].*\)\.081.*"

so if you have these files:

# ls la*
la1111.081.Z la2222.025 labcd laninfo laninfo2.sh
la11_22.081.Z la22F22.025 lancards laninfo.sh

the grep returns this:

# ls | grep "^la\([0-9].*\)\.081.*"
la1111.081.Z
la11_22.081.Z
#

live free or die
harry
Live Free or Die
SHABU KHAN
Trusted Contributor

Re: sh pattern matching problems

Hi Marc,

Try your find command without the quotes it works for me ...

find . -name la+([0-9]).081*

-Thanks,
Shabu
harry d brown jr
Honored Contributor

Re: sh pattern matching problems

Marc,

sorry, I misread your post, you only want this pattern:

la####.081*

so try this:

# ls | grep "^la\([0-9]\)\{4\}\.081.*"
la1111.081.Z
#

if you want this: la###...###.081*

then change the \{4\} to \{4,\} to say at least four digits but any more is ok also.


live free or die
harry
Live Free or Die
marc seguin
Regular Advisor

Re: sh pattern matching problems

Harry, thanks for having interest, and I will use something like you wrote.
But I don't know why the 'find' command doesn't work.
Shabu, I do need quotes, and your command doesn't work when there are more than one file
matching. (explanation in my last post, about substitution)
It looks like the find command doesn't accept "(" symbols, only "?", "*" and "[".
SHABU KHAN
Trusted Contributor

Re: sh pattern matching problems

Marc,

I think this will solve your purpose ...

find . -name "la[0-9]*.081*"

Thanks,
Shabu
marc seguin
Regular Advisor

Re: sh pattern matching problems

Sorry Shabu, but your first star (*) means "Anything after one figure".
So it matches la4zzz.081
Lothar Krueler
Regular Advisor

Re: sh pattern matching problems

Hi Marc,
I didn't find a way to use quantifier that works in the name option of the find command, but if you need it in a script try something like this:

The files are :
la1111.081.Z
la11_22.081.Z
la123.081.Z
la2222.025
la23.081.Z
la23x.081.Z
labcd

The command (in one line):
# find . \( -name "la[0-9][0-9].*.Z" -o
-name "la[0-9][0-9][0-9][0-9].*.Z" -o
-name "la[0-9][0-9][0-9].*.Z" \)

./la1111.081.Z
./la123.081.Z
./la23.081.Z

Hope This helps
Lothar
Wissen macht zaghaft, Dummheit kann alles!
Carlos Fernandez Riera
Honored Contributor
Solution

Re: sh pattern matching problems

Well... i have done several tests and :

1- ls la+([0-9]).081* # runs OK
2- find la+([0-9]).081* # runs OK because REGEXP is interpreted by sh

3- find . -name "la+([0-9]).081*" # never runs. Even man find says that expression will be interpreted by regexp, in facts it do not work, and so it must be a issue of find. IMMO.
unsupported
marc seguin
Regular Advisor

Re: sh pattern matching problems

Lothar, it works, but it is not exactly the same.....
Carlos, it could be the conclusion to the story !

I found an equivalent way to correspond to
#ls la+([0-9]).081*

find . -name "la*.081*" ! -name "la*[!0-9]*.081*"

Darrell Allen
Honored Contributor

Re: sh pattern matching problems

Hi Marc,

I've been trying to figure this out and can't. Hopefully someone else will.

I do know that you are correct to escape your regexp in the find command with quotes. Otherwise the shell will expand the regexp before passing it to find. The only files found would be ones with names that match those found in the current directory.

I'm still hoping for a resolution.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Steven Gillard_2
Honored Contributor

Re: sh pattern matching problems

My guess is that find only supports basic regular expressions, not the extended variety (see the regexp(5) man page). Hence the '+' character is not recognised as special because its only part of the extended regular expression syntax.

You might like to try the GNU version of find at:

http://hpux.connect.org.uk/hppd/hpux/Gnu/findutils-4.1.5/

Or alternatively pipe the output through grep -E.

Regards,
Steve
H.Merijn Brand (procura
Honored Contributor

Re: sh pattern matching problems

l1:/tmp 112 > ls -a la*
la111.081.Z la11_22.081.Z la2222.025 labcd
l1:/tmp 113 > perl -MFile::Find -le 'find(sub{m/^la\d+\.081/||return;print$File::Find::name},".")'
./la111.081.Z
l1:/tmp 114 >
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: sh pattern matching problems

GNU find does not support the '+'
Enjoy, Have FUN! H.Merijn