LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 10-03-2005, 02:04 PM   #1
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Rep: Reputation: 30
Reg Expression Question


I am trying to modify a reg expression built by a poster here.

Code:
msg=$(echo $line|sed 's/.*\] \(.*\)/\1/')
I am having trouble getting this to recognize files that have parentheses in the file name. Could someone help me out with that?
 
Old 10-03-2005, 02:18 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
It would be helpful if you could provide some examples of the text being processed and what you'd like the result to be.

As far as I can tell, the expression you give above doesn't look for a parenthesis at all. It looks for the last occurrence of a literal close square bracket ( ] ), ignores the space immediately after the bracket, and returns everything else in the line.

EDIT:
BTW, happy birthday!
 
Old 10-03-2005, 04:48 PM   #3
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Thanks for noticing my birthday.

Anyway here is an example of the text

Here is what the script is monitoring:
Code:
<10/03/05@17:38:04> [DECODE] Opened Majora's Mask - 42 - Magic Hag's House.mp3
<10/03/05@17:38:26> [MAIN] Title Updated
<10/03/05@17:39:34> [DECODE] Opened 32 - Daughter of Madain Sari.mp3
<10/03/05@17:39:57> [MAIN] Title Updated
<10/03/05@16:57:34> [DECODE] Opened 09 - The Sons (Feat. Brett Reilly) - Too Much Of A Good Thing.mp3
<10/03/05@16:57:56> [MAIN] Title Updated
<10/03/05@17:02:46> [DECODE] Opened 08 - Liberi Fatali (FF VIII).mp3

Here is an example of file names:

Code:
/video3/MythMusic/U2/U2 - The Best of 1980-1990/210 - Luminous Times (Hold on to Love).mp3
/video3/MythMusic/U2/U2 - The Best of 1980-1990/211 - Hallelujah Here She Comes.mp3
/video3/MythMusic/U2/U2 - The Best of 1980-1990/212 - Silver And Gold.mp3
/video3/MythMusic/Nickelback/Silver Side Up/01 Never Again.mp3
/video3/MythMusic/Nickelback/Silver Side Up/02 How You Remind Me.mp3
Most of the files do not have () or [] in them, but there are some that do, and throw off my reg expression.

Last edited by windisch; 10-03-2005 at 04:51 PM.
 
Old 10-03-2005, 11:20 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Ok, I may not have a complete grasp on the problem, because I'm not encountering a problem with filenames having parentheses in them. So double check what I'm doing here. Here we go...

The expression, as you have it above, modifies the monitored data you have to this:
Code:
Opened Majora's Mask - 42 - Magic Hag's House.mp3
Title Updated
Opened 32 - Daughter of Madain Sari.mp3
Title Updated
Opened 09 - The Sons (Feat. Brett Reilly) - Too Much Of A Good Thing.mp3
Title Updated
Opened 08 - Liberi Fatali (FF VIII).mp3
That's the result I get from running the command on my system. So that's what I'm going to work with. If that's not the output you want, then we'll need to tinker with the expression some more.

I noticed the data alternates between "[DECODE]" and "[MAIN]" and those can serve as markers to prevent the "greedy" nature of an RE from getting too much. The modified version is:
Code:
msg=$( echo $line | sed 's%.*\[\(\(DECODE\)\|\(MAIN\)\)\] \(.*\)%\4%'
A couple things, I changed the format from "s///" to "s%%%". It's a little dizzying (to me) to see forward slashes and backslashes mingling. I'm not goinf to go so far as to say the new expression is clearer because of it, but oh well. Also, I added spaces to to suit my style.

So what does this beast of an expression do? It ignores anything until it comes to an open-close set of square brackets that contain either "DECODE" or "MAIN". It ignores the space immediately after the close square bracket, and returns everything following that space to the end of the line. So, on a fictitious data set of:
Code:
<10/03/05@17:38:04> [DECODE] Opened Majora's Mask - 42 - Magic Hag's House.mp3
<10/03/05@17:38:26> [MAIN] Title Updated
<10/03/05@17:39:34> [DECODE] Opened 32 [disc 1]- Daughter of Madain Sari.mp3
<10/03/05@17:39:57> [MAIN] Title Updated
<10/03/05@16:57:34> [DECODE] Opened 09 [disc 20] - The Sons (Feat. Brett Reilly) - Too Much Of A Good Thing.mp3
<10/03/05@16:57:56> [MAIN] Title Updated
<10/03/05@17:02:46> [DECODE] Opened 08 [disco [pt2]] - Liberi Fatali (FF VIII).mp3
The result would be:
Code:
Opened Majora's Mask - 42 - Magic Hag's House.mp3
Title Updated
Opened 32 [disc 1]- Daughter of Madain Sari.mp3
Title Updated
Opened 09 [disc 20] - The Sons (Feat. Brett Reilly) - Too Much Of A Good Thing.mp3
Title Updated
Opened 08 [disco [pt2]] - Liberi Fatali (FF VIII).mp3
So we're back at the original output except the expression doesn't get hung up on square brackets later in the filename. Like I said, if that's not exactly what you're looking for, we can refine the expression. It'll take some desired intput->output examples to flesh out the expression.
 
Old 10-04-2005, 06:37 AM   #5
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
You're right, I changed my script to look and see what the reg expression, and it doesn't have a problem with (), it does with []. I assumed that it was a problem with parenthese because when I request a song, it loops on songs that have parenthese. My problem must be with the awk command I use to remove the song from my playlist. I would appreciate your help to get this to recognize brackets. The only thing I need to change in your code, is that I don't need to grab the word Opened. My goal is to grab the entire file name after Opened.
 
Old 10-04-2005, 10:56 AM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Ok, so you don't even want the "Title updated" business either, right? Ok, I was thinking this script was pulling off the date & time info and handng the stripped text to some other utility. Actually, if it's going to awk, then it is, so I was right It's just not ripping off enough... Easily remedied...

Try this on for size:
Code:
msg=$( echo $line | sed 's%[^]]*\] \(\(Title Updated\)\|\(Opened \)\)\(.*\)%\4%'
Since it's chenged a bit from the previous version (same basic premise though), I'll give a quick run-down:
  1. Match any non-] character until the first literal ] is reached => [^]]*\]
  2. Ignore the space after ]
  3. Match explicit text: "Title Updated" or "Opened " (notice the space with Opened) => \(\(Title Updated\)\|\(Opened \)\)
  4. Match, and save, any text following the last either-or match => \(.*\)
  5. Replace everything with the saved text
So this RE should just give you the filename, or a blank empty line (in the case of a "Title Updated" entry). I gave it a shot with two different input lines (i.e. not thorough testing), and it worked as expected. So give it a test drive, and let me know if it needs anymore tweaking.
 
Old 10-04-2005, 11:08 AM   #7
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Thanks, I'll see how it works.
 
  


Reply



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
Nice surprise Kaffeine plays Reg 1 and Reg 2 disks 1kyle SUSE / openSUSE 1 10-10-2005 04:47 PM
Regular expression question - John Sloan Linux - Software 1 09-08-2004 12:33 PM
'find' expression question Thorsten Linux - General 3 05-03-2004 07:41 AM
Simple expression question shaggz Linux - General 2 11-20-2003 11:30 AM
regular expression question Gantrep Linux - Software 2 04-20-2003 04:24 PM

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

All times are GMT -5. The time now is 10:26 PM.

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
Open Source Consulting | Domain Registration