LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 05-05-2010, 09:08 PM   #1
bdoremus
LQ Newbie
 
Registered: May 2010
Posts: 12

Rep: Reputation: 0
Question find with a regex path


I'm new to bash scripting and regexp, so sorry if this is a simple question.

I'm using bash scripting to find any file that matches a path governed by the following regular expression:
"(monthly|nightly).[0-9]+/home/(user1|user2)/.mailbox/"
to match files like:
monthly.9/home/user1/.mailbox/l23131564
nightly.15/home/user2/.mailbox/cur/6546213

I've tried:
Code:
myRegex="(monthly|nightly).[0-9]+/home/(user1|user2)/.mailbox/"
find ./ -regextype posix-egrep -regex $myRegex
and it just spins and never gives me an answer, even though the file structure isn't that big.

what am I missing?
 
Old 05-05-2010, 09:19 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
This works for me:
Code:
myRegex='.*(monthly|nightly).[0-9]+/home/(user1|user2)/.mailbox/.*'
find ./ -regextype posix-egrep -regex $myRegex
Bear in mind that with regex:
Code:
 "This is a match on the whole path, not a search."

Cheers,
Tink

Last edited by Tinkster; 05-05-2010 at 09:21 PM.
 
Old 05-05-2010, 09:20 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,509

Rep: Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894
Have you tried it without using a variable?
 
Old 05-05-2010, 09:22 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by grail View Post
Have you tried it without using a variable?
The variable works just fine ;}
 
Old 05-05-2010, 09:40 PM   #5
bdoremus
LQ Newbie
 
Registered: May 2010
Posts: 12

Original Poster
Rep: Reputation: 0
I've narrowed it down to the OR that is giving me problems.

I tried creating a simple file structure with some blank files in it, but this doesn't return any matches.
Code:
# mkdir ./test
# mkdir ./test/folder1
# mkdir ./test/folder2
# touch ./test/folder2/file1
# touch ./test/folder2/file2
# mkdir ./test/folder1/file3
# mkdir ./test/folder1/file2
# find ./ -regextype posix-egrep -regex "./test/(folder1|folder2)/"
#
I've also tried giving the regex the full path "/home/user/Desktop/(folder1|folder2)/" to no avail.

Help?!

Last edited by bdoremus; 05-05-2010 at 09:54 PM.
 
Old 05-05-2010, 09:53 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Look at my first response ... it's NOT the or, it's the missing ".*" at the beginning/end,
or the fact that you're not searching from the location where your absolute path starts.

Looking at your example above,
Code:
$ cd tmp/
$ mkdir -p test/dir1
$ mkdir -p test/dir2
$ touch test/dir1/file1
$ touch test/dir1/file2
$ touch test/dir2/file3
$ touch test/dir2/file4
$ find ./ -regextype posix-egrep -regex "./(dir1|dir2)/"
$
$ find ./ -regextype posix-egrep -regex ".*/(dir1|dir2)/.*"
./test/dir1/file1
./test/dir1/file2
./test/dir2/file3
./test/dir2/file4
$

Cheers,
Tink
 
1 members found this post helpful.
Old 05-05-2010, 09:58 PM   #7
bdoremus
LQ Newbie
 
Registered: May 2010
Posts: 12

Original Poster
Rep: Reputation: 0
wow, I've spent hours on this.

Thanks, Tink!!!!!
 
Old 05-05-2010, 10:24 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Pleasure ... ;}


Feel free to push the thanks button. :D
 
  


Reply

Tags
find, path, regex


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
[SOLVED] regex match string from start to find unique combinations fukawi2 Programming 6 02-11-2010 05:32 PM
Find a file path and directory path ak.lokesh Linux - Newbie 3 02-19-2009 12:37 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Quick regex problem, can't find solution R00ts Programming 3 05-25-2005 02:55 PM
Java can't find package, but PATH set correctly (j2se1.4.0, java.util.regex package) Ethan Programming 5 02-06-2004 09:55 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration