Shell script to build a list of karaoke music files sorted by artist and title!
Linux - NewbieThis 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
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.
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".
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);
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
Last edited by Kenhelm; 08-12-2008 at 03:13 PM.
Reason: Removed -e's from sed statements
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.