LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sed wizard wanted to write oneliner (https://www.linuxquestions.org/questions/linux-newbie-8/sed-wizard-wanted-to-write-oneliner-755119/)

shy_guest 09-14-2009 12:07 PM

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.

lutusp 09-14-2009 05:30 PM

Quote:

Originally Posted by shy_guest (Post 3682471)
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.

Tinkster 09-14-2009 06:55 PM

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.

shy_guest 09-14-2009 11:26 PM

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.

lutusp 09-15-2009 02:24 AM

Quote:

Originally Posted by shy_guest (Post 3683043)
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!

Tinkster 09-15-2009 05:25 AM

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).

shy_guest 09-15-2009 04:11 PM

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.

Tinkster 09-15-2009 09:25 PM

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


All times are GMT -5. The time now is 03:35 AM.