LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
LinkBack Search this Thread
Old 08-12-2008, 02:39 AM   #1
imix40
LQ Newbie
 
Registered: Jul 2008
Location: Baja, Mexico
Distribution: Ubuntu
Posts: 2

Rep: Reputation: 0
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!
 
Old 08-12-2008, 02:57 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
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
 
Old 08-12-2008, 02:43 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 328

Rep: Reputation: 140Reputation: 140
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

Last edited by Kenhelm; 08-12-2008 at 03:13 PM. Reason: Removed -e's from sed statements
 
Old 08-13-2008, 06:11 PM   #4
imix40
LQ Newbie
 
Registered: Jul 2008
Location: Baja, Mexico
Distribution: Ubuntu
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 08-13-2008, 07:10 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 328

Rep: Reputation: 140Reputation: 140
You need extra spaces inside the brackets. Try this
Code:
if [[ $ext = $MP3 ]]; then
 
  


Reply

Tags
filenames, list, shell script


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A program dowloading automatic the lyrics of music, it reads title with xmms-shell frenchn00b Linux - General 4 02-13-2008 03:25 PM
How do I produce sorted list of files in current directory over a certain size? bangback Linux - Newbie 3 10-25-2006 12:11 AM
Amarok sorting song list - Artist THEN Title? ironfistchamp Linux - Software 0 06-02-2006 05:56 PM
add "Artist" and "Album Title" to mp3 files powah Linux - Software 2 04-05-2005 03:04 AM
File>> Artist>> Title>> Time>> Size amjad General 1 02-02-2005 03:30 PM


All times are GMT -5. The time now is 06:39 AM.

Main Menu
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