LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script to build a list of karaoke music files sorted by artist and title! (http://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-build-a-list-of-karaoke-music-files-sorted-by-artist-and-title-662172/)

imix40 08-12-2008 03:39 AM

Shell script to build a list of karaoke music files sorted by artist and title!
 
Hi.

I have a limited knowledge of shell scripting, and I will appreciate any help I can get to create a shell script (*.sh; or a perl one) to do the following.

I have in a given directory karaoke files like:

all you need is love - the beatles.cdg
all you need is love - the beatles.mp3
....
....
with or without you - u2.cdg
with or without you - u2.mp3


The script is required to do:

(1) Rename the files as:

All you need is love - The Beatles.cdg
...
...
With or without you - U2.mp3


[i.e. First letter of title be capital, as well as the first of the artist's full name]


(2) Create a file that can be opened with, for example, OpenOffice spreadsheet, to display the songs in the directory; e.g. "songs.ods". The file "songs.ods" should have in one column the "title" and in the other the "artist name". This is so I can print out a song-book arranged by "title" and other by "artist name".


I have managed to do some minor things to the files in the directory; for example, eliminating the first 3 characters of a file, so that "01 all you need is love - the beatles.cdg" turns into "all you need is love - the beatles.cdg". But I am having problems with the renaming and building of a "song-book".

Hopefully you can lend me a hand. Thanks!

chrism01 08-12-2008 03:57 AM

Its prob do-able in shell, but I'd use Perl. Here's a few bits of code to get you started
Code:

open(OUTF, ">", "song_artist.txt") or die "can't open file song_artist.txt $!";

opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($file = readdir(DIR)))
{
    next if $file =~ /^\.\.?$/;    # skip . and ..

    # do something with "$dirname/$file"
    @file_parts = split(/-/, $file);
   
    #$file_parts[0] = song, [1] =artist
    $song = ucfirst($file_parts[0]);
   
    @name_parts = split(/ /, $file_parts[1]);
    for $name ( @name_parts )
    {
        ucfirst($name);
    }

    # now you just concat that lot and use the mv/rename cmd
    # then print song, artist to file
}
closedir(DIR);
close(OUTF);

http://perldoc.perl.org/index.html

Kenhelm 08-12-2008 03:43 PM

This uses bash and GNU sed
Code:

filename="all you need is love - the beatles.mp3"
ext=$(echo $filename | grep -o '\....$')
title=$(echo $filename | sed 's/ - .*//; s/\b\w/\u&/')
artist=$(echo $filename | sed "s/.* - \(.*\)$ext/\1/; s/\b\w/\u&/g")
echo $title - $artist$ext

All you need is love - The Beatles.mp3


imix40 08-13-2008 07:11 PM

almost there!!
 
Hi. I really appreciate your comments and suggestions to solve my problem. I have written, with your help, the following rudimentary *.sh file:

----------------------------------------------------------
#!/bin/bash

header1=$(echo "Title, Artist" )
header2=$(echo "Artist, Title" )

echo $header1 > "TitleArtist.csv"
echo $header2 > "ArtistTitle.csv"

MP3=$(echo ".mp3")

for filename in *.mp3 *.cdg
do
ext=$(echo $filename | grep -o '\....$')
title=$(echo $filename | sed 's/ - .*//; s/\b\w/\u&/')
artist=$(echo $filename | sed "s/.* - \(.*\)$ext/\1/; s/\b\w/\u&/g")
newfile=$(echo $title - $artist$ext)
mv "$filename" "$newfile"
TA=$(echo $title, $artist)
AT=$(echo $artist, $title)
if [[$ext = $MP3]]; then
echo $TA >> "TitleArtist.csv"
echo $AT >> "ArtistTitle.csv"
else
continue
fi
done
------------------------------------------------

This generates two comma separated value files, that I have no problem when opening can with the spreadsheet of OpenOffice (using the text CSV option).

The only problem that I have with the script, as coded above, is in the IF loop. This I did so to not have two "DO-LOOPs", one for the *.cdg and one for the *.mp3 files. Separating in 2 DO-LOOPs works fine since in only one of them I put the output instructions. I can live with that, but ....well, doesn't look nice :-)

When running the above script it outputs an error like:

[[.mp3: not found
[[.cdg: not found

I guess I am missing something about the syntaxis in the IF loop or definitions of variables. I will appreciate some extra help here.

Cheers,

Hector

Kenhelm 08-13-2008 08:10 PM

You need extra spaces inside the brackets. Try this
Code:

if [[ $ext = $MP3 ]]; then


All times are GMT -5. The time now is 02:20 PM.