LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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

Reply
 
Search this Thread
Old 05-08-2008, 12:47 PM   #1
lumix
Member
 
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323

Rep: Reputation: 30
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?
 
Old 05-08-2008, 01:05 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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.
 
Old 05-08-2008, 01:08 PM   #3
lumix
Member
 
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323

Original Poster
Rep: Reputation: 30
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.
 
Old 05-08-2008, 01:09 PM   #4
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by lumix View Post
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.
 
Old 05-08-2008, 01:22 PM   #5
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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...)
 
Old 05-08-2008, 01:38 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,490

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Why not simply...?
Code:
find . -name \*.mp3 -o -name \*.aac
 
Old 05-08-2008, 02:00 PM   #7
lumix
Member
 
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
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.
 
Old 05-08-2008, 02:20 PM   #8
lumix
Member
 
Registered: Mar 2007
Distribution: Hardy (Gnome on Ubuntu 8.04) on Compaq N600c laptop
Posts: 323

Original Poster
Rep: Reputation: 30
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.
 
Old 05-08-2008, 02:48 PM   #9
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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.
 
Old 05-09-2008, 01:11 AM   #10
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by lumix View Post
...
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.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
fdisk reports odd "Start "and "End" sectors on single partition eponymous Linux - Software 3 10-01-2007 03:41 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
add "Artist" and "Album Title" to mp3 files powah Linux - Software 2 04-05-2005 03:04 AM


All times are GMT -5. The time now is 11:25 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