LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2005, 01:27 AM   #1
aecaudel
LQ Newbie
 
Registered: Nov 2004
Distribution: Gentoo
Posts: 7

Rep: Reputation: 0
Need help with regular expression


I am trying to use find (under linux) to match file names of the form <anything>_[0-9]. For example:

text_0.txt
.foo_19.bar.c
etc.

I am using:

find . -regex '^\.*_[0-9]+' -print

So far it will only match the filename if it is preceeded by a "."

What am I doing wrong?
 
Old 11-03-2005, 04:45 AM   #2
fvgestel
LQ Newbie
 
Registered: Oct 2005
Location: Netherlands
Posts: 21

Rep: Reputation: 15
the backslash is making the dot go literal. This should work :
Code:
find . -regex '_[0-9]+\.' -print
 
Old 11-03-2005, 01:59 PM   #3
aecaudel
LQ Newbie
 
Registered: Nov 2004
Distribution: Gentoo
Posts: 7

Original Poster
Rep: Reputation: 0
Nope, doesn't work.

Actually the "\" in my first post wasn't necessary. Even without it, the regex only finds filenames with an initial "." It's as if the dot is being escaped even without the slash.
 
Old 11-03-2005, 02:34 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,235

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
my aix version of 'find' doesn't like regex.

Code:
schneidz@lq:/temp> find . -regex '_[0-9]+\.' -print
find: 0652-017 -regex is not a valid option.
if you dont mind grep this does it for me:
Code:
schneidz@lq:/temp> find . | grep _[0-9]
./.temp_5.tmp
./dupe/schneidz_11-03-05-report2.tmp
./temp_51.tmp
./hello_2.tmp
./world_23.tmp
peace
 
Old 11-03-2005, 03:29 PM   #5
aecaudel
LQ Newbie
 
Registered: Nov 2004
Distribution: Gentoo
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks, schneidz, that does it!

Although, for my own information, It would be instructive if someone could tell me why the -regex as above doesn't work.
 
Old 11-04-2005, 02:40 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,432

Rep: Reputation: 203Reputation: 203Reputation: 203
have you heard of man ?




Code:
       -regex pattern
              File  name  matches regular expression pattern.   This is a match
             on the whole path, not a search.  For example, to match  a  file
              named `./fubar3', you can use the regular expression `.*bar.' or
              `.*b.*3', but not `f.*r3'.  The regular  expressions  understood
              by find are by default POSIX Basic Regular Expressions, but this
              can be changed with the -regextype option.
 
Old 11-04-2005, 05:28 AM   #7
fvgestel
LQ Newbie
 
Registered: Oct 2005
Location: Netherlands
Posts: 21

Rep: Reputation: 15
Yes, I also noticed that the whole path is matched. Not only that, find adds a carot at the start and a dollar sign at the end of the expression. So the following regexp SHOULD work :
Code:
find . -regex '.*_[0-9]+\.[^/]*'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 07:48 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM
regular expression gumby Programming 3 07-15-2003 12:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration