Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-08-2008, 12:47 PM
|
#1
|
Member
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323
Rep:
|
A single regex to match anything with ".aac" or ".mp3" at the end ?
Tried ".*/.[\baac\b\bmp3\b]$" but no go. Tried many variations of this?
Any ideas?
|
|
|
05-08-2008, 01:05 PM
|
#2
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
Depends on what you're using.
For egrep it is easy so piping into egrep might be the way to go:
<whatever> |egrep "aac$|mp3$"
The $ tells it to look for it at the end of the line.
|
|
|
05-08-2008, 01:08 PM
|
#3
|
Member
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323
Original Poster
Rep:
|
Needs to be a posix regexp
I won't bore you with the details, but my solution has to use "find -regex".
Thanks for the tip though...didn't know grep could do that.
|
|
|
05-08-2008, 01:09 PM
|
#4
|
Senior Member
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Rep:
|
Quote:
Originally Posted by lumix
Tried ".*/.[\baac\b\bmp3\b]$" but no go. Tried many variations of this?
Any ideas?
|
By using that regex you are asking machine for a string that consits any number characters, followed by a single character from a set "ac\bmp3" and has newline at the end.
By the way, where do you want to use regex?
You can use extended regular expressions:
Code:
ls|egrep "^.*\.(aac|mp3)$"
ls|grep "^.*\.\(aac\|mp3\)$"
or (ugly way, useful only if regexps with brackets are not supported)
Code:
ls |grep "^.*\.[acmp3]\{3\}$"
notice, that ^ and $ are not really necessary.
|
|
|
05-08-2008, 01:22 PM
|
#5
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
find . -name "*[am][ap][c3]"
This looks for any file that ends with a or m in 3rd from last character, a or p in 2nd from last and c or 3 in last. It will match the files you want but might match other oddities if they are there (e.g. aa3, mpc, apc...)
|
|
|
05-08-2008, 01:38 PM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Why not simply...?
Code:
find . -name \*.mp3 -o -name \*.aac
|
|
|
05-08-2008, 02:00 PM
|
#7
|
Member
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
Why not simply...?
Code:
find . -name \*.mp3 -o -name \*.aac
|
Again, the sordid details. Add to these details that I'd also like very much to learn more about the power of regular expressions.
My regex is in a php script, so while the other solution is very cool and creative "[am][ap][c3]", I need to be able to insert new file extensions by variable into my regex string. In other words, I need to be able to insert "avi" into this regex pattern string, and then use it in my find command to look for new file types. I'm pretty sure this is possible, and for various reasons I must use find and regexes.
Thanks for the response so far, btw.
|
|
|
05-08-2008, 02:20 PM
|
#8
|
Member
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323
Original Poster
Rep:
|
An answer: but why the need to escape so many chars?
The following is a fairly concise and easy to change solution. But why do I have to escape the so-called "round-brackets"?
me@localhost:/AV/Sea Change$ find -regex ".*\.\(aac\|mp3\)"
It very nicely ignores, for example, ./testaac or mp3song.txt.
|
|
|
05-08-2008, 02:48 PM
|
#9
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
Because different tools (including the shell) interpret the characters seen. The shell treats parentheses as "grouping" of commands and the vertical bar as a "pipe". It would therefore think you were attempting to pipe command aac into command mp3 and would attempt to do that BEFORE the rest of the line due to the grouping. Escaping and quoting is one of the most maddening things you'll deal with in scripting.
There is a command line I've used that actually quotes escapes and escapes quotes to work correctly. It seems to be nonsensical to do that but on a command line where you're in a shell and piping things into awk and/or grep it sometimes is necessary to do things like this to be sure one thing treats it literally and passes it on that way to something else that you want to interpret it.
|
|
|
05-09-2008, 01:11 AM
|
#10
|
Senior Member
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Rep:
|
Quote:
Originally Posted by lumix
...
But why do I have to escape the so-called "round-brackets"?
...
|
To point out that they are special symbols, not the characters that must exist in the string.
There is a regex tutorial, you might want to read it: http://www.grymoire.com/Unix/Regular.html.
|
|
|
All times are GMT -5. The time now is 09:29 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|