Solved

using regular expression with slocate

Posted on 2007-12-04
12
1,408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 27

Expert Comment

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

Expert Comment

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

Expert Comment

by:omarfarid
ID: 20408532
Try

AA??????.txt
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 9

Expert Comment

by:ghostdog74
ID: 20408650
if you only want to find files starting with AA....

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

Author Comment

by:hpasystems
ID: 20408799
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
ID: 20408806
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
 

Author Comment

by:hpasystems
ID: 20408852
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
ID: 20408887
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
ID: 20408895
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
ID: 20408908
From the man page it would seem you should skip regex altogether:
locate AA*.txt
0
 

Author Comment

by:hpasystems
ID: 20409040
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
ID: 31412766
Great response time.
Very helpful.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

624 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