Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
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.
|
 |
01-04-2009, 09:39 AM
|
#1
|
Member
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68
Rep:
|
Another Sed challenge
I'm trying to create a script that takes a file full of names of files (mp3's actually) with the following syntax:
Mon_Da_YEAR_Sub_ject_Sub_ject_Location_side_1_or_2.mp3
...and makes folders from those file names in the following syntax:
Location-YEAR-MN-DY- Subject -BLH
The "BLH" part needs to be indiscriminately attached to every folder name.
Basically it'll take some Regex to parse the data from the file name and reorganize it into the folder name...and I think there will have to be a loop statement like do-while...can anyone help me?
|
|
|
01-04-2009, 04:01 PM
|
#2
|
Moderator
Registered: May 2001
Posts: 29,417
|
Quote:
Originally Posted by ifeatu
I'm trying to create a script
|
Unless reinventing the wheel is something you just got to do, did you check LQ, Freshmeat or Sourceforge for apps that already can do that? Besides filenames like "Mon_Da_YEAR_Sub_ject_Sub_ject_Location_side_1_or_2.mp3" aren't reliable and easy to work with (wrt IFS). If MP3s are tagged "right" then getting the mp3info and working on that would be *way* "safer" IMHO. The "BLH" part isn't something you would even need sed for. Maybe it's best to post whatever (pseudo) script you've got right now?..
Quote:
Originally Posted by ifeatu
Basically it'll take some Regex to parse the data from the file name and reorganize it into the folder name...
|
No challenge for you as it seems you recently bought ISBN 9780596528126 :-]
|
|
|
01-05-2009, 09:00 PM
|
#3
|
Member
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68
Original Poster
Rep:
|
okay...so I took your advise and took a stab at it...I ended up having to use a batch file in DOS for the folder creation part...then I installed GNU Sed for DOS and took a stab at the RegEx...here is where I got stuck:
OKay...first, here is some of my raw data:
April_14_1991_Bread_of_life_Vallejo_side_1.mp3
April_14_1991_Bread_of_life_Vallejo_side_2.mp3
April_21_1991_Ministry_zadok_priesthood_Vallejo_side_1.mp3
April_21_1991_Ministry_zadok_priesthood_Vallejo_side_2.mp3
Apr_05_1992_Matthew_13_Sower_Berkeley_side_1.mp3
Apr_05_1992_Matthew_13_Sower_Berkeley_side_2.mp3
Aug_04_1991B_Moving_out_alive_soul_Vallejo_side_1.mp3
Aug_04_1991B_Moving_out_alive_soul_Vallejo_side_2.mp3
Aug_04_1992_Daniel_7_Berkeley_side_1.mp3
Aug_04_1992_Daniel_7_Berkeley_side_2.mp3
So you have the month (.*) followed by the day ([0-9]{1,2}) followed by the year ([0-9]{4}) followed by a subject (.*) followed by a location (Vallejo,vallejo, Berkeley,berkeley, UC, Union City) followed by the word "side" followed by a 1 or 2
sed -r 's/(.*)([0-9]{2})\s*_\s*([0-9]{4})\s*_\s*(.*)\s*([vallejo][.*])\s*(side)\s*_\s*([1,2])(.*)/\3-\1-\2-\4/' test2.txt
> test3.txt
I think its pretty obvious where I'm running into my problem, I can't seem to get my syntax right for the location...can anyone help?
The line above returns absolutely nothing, it returns the data precisely as it is read.
|
|
|
01-06-2009, 03:17 AM
|
#4
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
That's a bit advanced for me, but I notice a couple of things:
1. in your data you have Vallejo, your sed uses vallejo (case is different)
2. Aug_04_1991B_Moving_out_alive_soul_Vallejo_side_1.mp3 & Aug_04_1991B_Moving_out_alive_soul_Vallejo_side_2.mp3 do not have an underscore after the year component.
|
|
|
01-06-2009, 05:00 AM
|
#5
|
Member
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68
Original Poster
Rep:
|
Quote:
Originally Posted by chrism01
That's a bit advanced for me, but I notice a couple of things:
1. in your data you have Vallejo, your sed uses vallejo (case is different)
2. Aug_04_1991B_Moving_out_alive_soul_Vallejo_side_1.mp3 & Aug_04_1991B_Moving_out_alive_soul_Vallejo_side_2.mp3 do not have an underscore after the year component.
|
therein lies a portion of the challenge. The regex needs to make provisions for the B or A that some times delimits the year...I understand that the data isn't at all pretty...but in this circumstance, reinventing the wheel is an absolute neccesity.
|
|
|
01-06-2009, 05:13 AM
|
#6
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,422
|
sed is best (only ?) for well-formed data.
Use something like perl - that way you can use some real programming techniques.
|
|
|
All times are GMT -5. The time now is 06:45 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
|
|