LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 09-14-2009, 01:07 PM   #1
shy_guest
LQ Newbie
 
Registered: Sep 2009
Location: In another land where the breeze and the trees and flowers glow blue
Distribution: Ubuntu 9.04, distrohopper
Posts: 11

Rep: Reputation: 0
Sed wizard wanted to write oneliner


I downloaded the Complete Buddy Holly by Purple Chick.

Unfortunately there are 10 directories with embedded blanks in the name (disc 1, ..., disc 10). & the 40 or so files in each directory are named Track 01.flac, Track 02.flac, etc which is pretty useless when you want to play the songs.

I want to have a bash script to rename the flac files using a file containing titles of the songs disc by disc.

My input title file is formatted like this (I used gedit to remove embedded spaces).

01:_Little_Baby
02:_(You're_So_Square)_Baby_I_Don't_Care
99:_Long_Complicated_Title(Perhaps_With_Special_Characters)

What I want as output for my bash script file is

mv Track\ 01.flac 01_Little_Baby.flac
mv Track\ 02.flac 02_(You're_So_Square)_Baby_I_Don't_Care.flac
mv Track\ 99.flac 99_Long_Complicated_Title(Perhaps_With_Special_Characters).flac

or better still

echo "mv Track\ 01.flac 01_Little_Baby.flac"
echo "mv Track\ 02.flac 02_(You're_So_Square)_Baby_I_Don't_Care.flac"
echo "mv Track\ 99.flac 99_Long_Complicated_Title(Perhaps_With_Special_Characters).flac"

so I can check it all works before running the script.

So far my bash script goes from directory to directory.

What I need now is a script wizard to write a sed oneliner that changes the title file so that I can insert the mv lines in the bash script.

Titles may contain brackets, apostrophes, question marks, maybe even exclamation marks. Do they need a backslash added to prevent interpretation as metacharacters by bash (or sed) ? How to duplicate the track number so it can make both the arguments for mv ?

Thanks to anyone who can crack this in a minute instead of me finding out by reading Bruce Barnett's sed doc for hours.
 
Old 09-14-2009, 06:30 PM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by shy_guest View Post
I downloaded the Complete Buddy Holly by Purple Chick.

Unfortunately there are 10 directories with embedded blanks in the name (disc 1, ..., disc 10). & the 40 or so files in each directory are named Track 01.flac, Track 02.flac, etc which is pretty useless when you want to play the songs.

I want to have a bash script to rename the flac files using a file containing titles of the songs disc by disc.

[ snip ]

What I need now is a script wizard to write a sed oneliner that changes the title file so that I can insert the mv lines in the bash script.
Actually, no, you do not want a "sed oneliner", in fact, you do not want to use "sed" at all. You want a Bash script that you can understand, and that will solve the problem -- both.

There are important questions that you haven't answered:

1. How is the track title file indexed to correspond to the .flac files? Is it by number, as in your example, or by line, or both?

2. How is the title file set up to correspond to the individual music directories? Is there a different title file for each directory, or is there a master title file?

3. Do the directory names correspond to album names, or are they arbitrary and uninformative?

Without more information, I can give you the barest outline of what you are after:

Code:
path="/path/of/interest"

find $path -type d | while read dir
do
   echo "Directory $dir:"
   find $dir -type f | grep -P "\.flac$" | while read fpath
   do
      fname=`basename $fpath`
      echo "Directory $dir, file $fname"
   done
done
At the moment this just scans directories and lists .flac files (notice that it separates the directory and file scans). It should be able to read and index the title file (and remove any funny characters from their names), but for that, more information is needed. Then we can attack the problem of correlating .flac files and their titles.
 
Old 09-14-2009, 07:55 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
If I understand the OP correctly he's after something like this; since
the spec isn't very clear I've assumed for simplicities sake that the
list of desired names is in the same directory that has the flacs.

Code:
#!/bin/bash
cat list | while read line
do
  number=$( echo $line | cut -d : -f1 )
  echo mv "Track ${number}.flac" "${line}.flac"
done
If that works it's next to trivial to use a wrapper-script,
maybe with find as suggested above, to iterate over the whole
structure with undesirable file-names.


Cheers,
Tink


P.S.: sed would have worked just fine, but cut is smaller/quicker
in this particular case.

Last edited by Tinkster; 09-14-2009 at 07:57 PM.
 
Old 09-15-2009, 12:26 AM   #4
shy_guest
LQ Newbie
 
Registered: Sep 2009
Location: In another land where the breeze and the trees and flowers glow blue
Distribution: Ubuntu 9.04, distrohopper
Posts: 11

Original Poster
Rep: Reputation: 0
lutusp & tinkster - Apologies for my inadequate spec. I'll try to be clearer in future.

There is just one file for the titles.
It contains 10 lists each with the format described in numerical order with one title per line.
At the head of each list I have inserted "cd /path of directory for relevant list" & some comment lines.
The album/directory names are just "disc 1", ..., "disc 10".

lutusp: I understand your script - I'm sure it will come in handy here (added to Tinkster's) & in future.
Tinkster : My Linux Phrasebook doesn't even list cut in the index so I don't understand your solution yet, but I will in a few minutes.

Maybe you can suggest a better book for me.
 
Old 09-15-2009, 03:24 AM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by shy_guest View Post
lutusp & tinkster - Apologies for my inadequate spec. I'll try to be clearer in future.

There is just one file for the titles.
It contains 10 lists each with the format described in numerical order with one title per line.
At the head of each list I have inserted "cd /path of directory for relevant list" & some comment lines.
The album/directory names are just "disc 1", ..., "disc 10".

lutusp: I understand your script - I'm sure it will come in handy here (added to Tinkster's) & in future.
Tinkster : My Linux Phrasebook doesn't even list cut in the index so I don't understand your solution yet, but I will in a few minutes.

Maybe you can suggest a better book for me.
So there is one master file with a directory name at the top of each of ten subsections with song titles, yes? If true, without actually having the master file and the real directory tree, there is no reliable way to create what you are after and be sure it will work. Sorry.

Also, the program will need to be written in Perl or Ruby -- this is beyond a reasonable bash script. The reason is that the master file's contents would need to be unambiguously partitioned dynamically as the directories were scanned, to access the right ten song titles for each directory.

Good luck!
 
Old 09-15-2009, 06:25 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I second the motion regarding the file-structure.

I don't know about the need for perl or ruby, I'm sure
awk could go a long way with this.



Cheers,
Tink


P.S.: As for the OPs question regarding reading material;
there's tons out there, and I haven't read any. I often
recommend scripting tutorial websites. There's one book
I do encourage people to buy, and that's because of its
quirky structure. It's full of cross-references, and
encourages the inquisitive mind to follow along.

O'Reilly "Unix Power Tools" (*not* the Linux version, it's
quite lame by comparison).
 
Old 09-15-2009, 05:11 PM   #7
shy_guest
LQ Newbie
 
Registered: Sep 2009
Location: In another land where the breeze and the trees and flowers glow blue
Distribution: Ubuntu 9.04, distrohopper
Posts: 11

Original Poster
Rep: Reputation: 0
Directory structure is really simple.

/home/me/Desktop/The_Complete_Buddy_Holly is the main path & then inside that there are disc 1, disc 2, ..., disc 10 containing target files & a couple of other directories for album covers & brochures. I think bash is up to that if the script file is correctly generated by a find. Or I can split the title file into 10.

Tink - I'd been browsing Unix Power Tools on Amazon but was also wondering whether to wait for new edition of Mark Sobell's book A Practical Guide to Linux(R) Commands, Editors, and Shell Programming which is due out in December.

BTW Your location tag gave me a belly laugh.
 
Old 09-15-2009, 10:25 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Heh ... glad you got a good laugh out of it.

I'll look up the book you're asking about in my copious spare
time ;} and come back with my views on if (if I can find it
somewhere around, that is).

Can you post a representative section of your control file,
something to work on?



Cheers,
Tink
 
  


Reply

Tags
sed


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
oneliner to launch an application in a specified directory wilsonsamm Linux - General 1 04-12-2009 11:43 AM
how do I write a script using sed to delete the first and last line in multiple files Rikki D Programming 4 05-03-2008 12:01 AM
read and write NTFS, opinions wanted jbrush Suse/Novell 14 02-22-2007 05:21 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
help with filesys oneliner chr15t0 Linux - General 3 08-24-2003 10:49 AM


All times are GMT -5. The time now is 02:22 AM.

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