using regular expression with slocate

Hello peeps,

I want to locate files matching a regular expression and can't get it working.

Example.....

locate files matching....

AAaabb11.txt
AAaacc11.txt
AAbbaa44.txt

by using: locate -r AA*.txt - or - locate --regexp=AA*.txt

I also tried locate --regexp=* AA*.txt

I have tried quoting and bracing but I'm not sure of what is required. man page is sparse.

Is locate able to match on specific expressions as per my example?

please help, I am not familiar with regular expressions other than what is already in place (learning by examples)

Cheers,
hpasystemsAsked:
Who is Participating?
 
omarfaridCommented:
Hi,

Quoted from man page:

"Patterns can contain
       shell-style metacharacters: `*', `?', and `[]'.      The metacharacters  do
       not  treat  `/'      or `.'      specially.  Therefore, a pattern `foo*bar' can
       match a file name that contains `foo3/bar', and a pattern `*duck*'  can
       match  a  file name that contains `lake/.ducky'.  Patterns that contain
       metacharacters should be quoted to protect them from expansion  by  the
       shell.

       If  a  pattern  is  a  plain string -- it contains no metacharacters --
       locate displays all file names in the database that contain that string
       anywhere.   If  a pattern does contain metacharacters, locate only dis-
       plays file names that match the pattern exactly.  As a result, patterns
       that  contain  metacharacters should usually begin with a `*', and will
       most often end with one as well.  The exceptions are patterns that  are
       intended to explicitly match the beginning or end of a file name."

"-r, --regex
            The  pattern specified on the command line is understood to be a
            regular expression, as opposed to a glob pattern.   The  Regular
            expressions  work  in  the same was as in emacs and find, except
            for the fact that "." will match      a  newline.   Filenames  whose
            full  paths  match  the specified regular expression are printed
            (or, in the case of the -c option, counted).   If  you  wish  to
            anchor  your  regular  expression  at  the ends of the full path
            name, then as is usual with regular expressions, you should  use
            the characters ^ and $ to signify this."

links:

http://unixhelp.ed.ac.uk/CGI/man-cgi?locate+1
http://www.hmug.org/man/1/locate.php
0
 
ddrudikCommented:
If you are looking for a regex pattern:
AA.*\.txt
0
 
ddrudikCommented:
And, more specifically, if your app supports it:
^AA.*\.txt$
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
omarfaridCommented:
Try

AA??????.txt
0
 
ghostdog74Commented:
if you only want to find files starting with AA....

find /path -type f -name "AA*txt" -print
0
 
hpasystemsAuthor Commented:
Hi, yes, I can use find, but want to use locate - or at least be familiar with using locate with a regular expression.

ddrudik,
So the locate command should look like....
locate --regexp=* AAA.*\.txt ??? This did not work.

Omarfarid,
Using AA??????.txt is no good, the variable can be different lengths.

Can anyone supply the exact syntax of using a regular expression for locate???

Thanks all for assist, please continue!  :)
0
 
omarfaridCommented:
So, what about AA*.txt?

Do you have any particular criteria for the file name? What you have shown is of a fixed length.

0
 
hpasystemsAuthor Commented:
yeah sorry, just realised my examples are misleading.

it needs to be like you say....
AA*.txt

...and how the command should actually look. I cannot get it to work. returns nothing.

locate -r AA*.txt  <- obviously, I am missing something.

0
 
ddrudikCommented:
the pattern I recommended was:
^AA.*\.txt$

Usually in applications etc, you either enclose patterns in quotes and/or some other bounding character such as /, but I cannot find an actual slocate regex example online so I can't say what's required there.

The regex pattern:
AA*.txt

says:
A = The literal character "A"
A* = The literal character "A" 0 or more times
. = Any character
txt = The literal characters "txt"

Note the format of the pattern I supplied above.
0
 
ddrudikCommented:
From the man page it would seem you should skip regex altogether:
locate AA*.txt
0
 
hpasystemsAuthor Commented:
Hi guys,

thanks for assist. My man pages are not very helpful, very baron. Also, the test machine isn't working but server I want it running on is and works with/without -r as suggested above. Only dirfference in versions...

did not work for...
Secure Locate 2.6 - Released May 21, 2001
on RHEL 2.1

did work for...
Secure Locate 2.7 - Released January 24, 2003
on RHEL4AS

Thanks guys, much appreciated.
0
 
hpasystemsAuthor Commented:
Great response time.
Very helpful.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.