Solved

using regular expression with slocate

Posted on 2007-12-04
12
1,387 Views
Last Modified: 2013-12-16
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,
0
Comment
Question by:hpasystems
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
If you are looking for a regex pattern:
AA.*\.txt
0
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
And, more specifically, if your app supports it:
^AA.*\.txt$
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
Try

AA??????.txt
0
 
LVL 9

Expert Comment

by:ghostdog74
Comment Utility
if you only want to find files starting with AA....

find /path -type f -name "AA*txt" -print
0
 

Author Comment

by:hpasystems
Comment Utility
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
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:hpasystems
Comment Utility
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
 
LVL 40

Accepted Solution

by:
omarfarid earned 40 total points
Comment Utility
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
 
LVL 27

Assisted Solution

by:ddrudik
ddrudik earned 35 total points
Comment Utility
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
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
From the man page it would seem you should skip regex altogether:
locate AA*.txt
0
 

Author Comment

by:hpasystems
Comment Utility
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
 

Author Closing Comment

by:hpasystems
Comment Utility
Great response time.
Very helpful.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now