MCWong
Blah! MCWong

20040708 Thursday July 08, 2004

ksh scripting #2: unix friendly filename

Every time I rip and encode tracks from my CD collections (yes I still buy them) to mp3, I had the need of getting a unix friendly filename from the human readable title since I use Solaris to do everything. I typically grab the titles from a free cddb out there that happens to have my album's details. But these come in various formats such as:


9. title of track nine
10) track-ten's title
11: track eleven title & nothing else...

While I need them to be unix friendly, such as:


9:title_of_track_nine
10:track-ten_s_title
11:track_eleven_title_+_nothing_else___

So I script it with the help of sed(1), and what a monster I've created!


#!/bin/ksh

cddb=$1

ttlist=`sed 's/^[   ]*//;/^$/d;/^[^0-9]/d;s/[	]*$//;s/[   ][	]*/ /g;\
/^[0-9][0-9]*[. _    :-)]/s/[. _ :-)][. _    :-)]*/:/;s/[".]//g;\
y/ '"'"'&\//__++/' $cddb`

#   remove leading spaces and tabs
#    s/^[   ]*//
#   remove blank lines
#    /^$/d
#   ignore lines not beginning with a number
#    /^[^0-9]/d
#   remove trailing spaces and tabs
#    s/[    ]*$//
#   compress 1 or more adjacent white spaces
#    s/[    ][	]*/ /g
#   replace characters delimiting track no. from the title, with a single delimiter
#    /^[0-9][0-9]*[. _	:-)]/s/[. _ :-)][. _	:-)]*/:/
#   remove characters that won't look good if transformed to "_"
#    s/[".]//g
#   transform space and special chars to shell friendly chars
#    y/ '&\//__++/

# create title[track] array elements
for tt in $ttlist
do
  let "track = ${tt%%:*}"
  title[$track]=${tt#*:}
done
    :

    :

I'm not even sure if I understand this now that I looked at it again. :(

The for loop that followed put the track title into an array indexed by the track no., nice and neat for whatever I want to do next.

ksh scripting flashback: #1

(2004-07-08 00:00:02.0) Permalink Comments [5]

Comments:

There's this great little perl script, originally by Larry Wall, later rewritten by Robin Barker, which can use any Perl expression to rename files. Which means you can do things like "rename 'y/A-Z/a-z/' *.mp3" to convert all your MP3 filenames to lower case, or, from your script: "rename 's/^[\t ]*//' *" to remove the tabs and spaces from the beginning. Here's the script: http://www.mail-archive.com/boston-pm@happyfunball.pm.org/msg01935/rename The -n option shows how files will be renamed, without actually doing the renames.

Posted by Bart Cortooms on July 08, 2004 at 03:25 AM PDT #

Can anyone tell me where I can get some cytosport muscle milk? I want to find some muscle milk because I am a body builder. I lift weights and exercise all the time while using vitamins. Muscle milk is the best formula.

Posted by muscle milk on November 17, 2004 at 11:23 AM PST #

Tell me where I can get some cytosport muscle milk? I want to find some muscle milk because I am a body builder. I lift weights and exercise all the time while using vitamins.

Posted by muscle milk on November 17, 2004 at 11:31 AM PST #

Soothing relief for persons suffering from eczema and psoriasis. No doctors prescription required!

Posted by Derma Care Solutions on December 08, 2004 at 12:57 PM PST #

You can buy Hydrocodone online at cheap prices without a prescription. Hydrocodone Online is availbale to Buy Hydrocodone. Also get Hydrocodone APAP from the site. Buy Hydrocodone Online from a real pharmacy and get Hydrocodone Without Prescription or a Hydrocodone Prescription can be provided. Generic Hydrocodone can save you money so Order Hydrocodone now and get Cheap Hydrocodone delivered to home. Buy Cheap Hydrocodone from the pharmacy. Now you can Buy Hydrocodone.

Posted by No Prescription Needed on December 17, 2004 at 12:29 AM PST #

Post a Comment:

Comments are closed for this entry.

archives
links
referers