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]


archives
links
referers