[PLUG] substitution question...

Fred James fredjame at fredjame.cnc.net
Tue Dec 9 19:20:44 UTC 2008


Eitan Tsur wrote:
> Ok, so here's a few examples... (from my music collection)
>
> Guns N' Roses
> FAT FREDDY'S DROP
> black_rebel_motorcycle_club
>
> The end results SHOULD be like so:
> Guns N' Roses
> Fat Freddy's Drop (note the 's is not capitalized)
> Black Rebel Motorcycle Club (underscores converted to spaces)
>   
Eitan Tsur
Oops - I made an incorrect assumption ... assuming that the file name 
would have underscores instead of spaces (as in the 
"black_rebel_motorcycle_club" example) this would work (see below) ... 
but it won't work with spaces (i.e., "FAT FREDDY'S DROP")
But you could use 'ls -1' to make a text file of names, (one name per 
line/record) and then feed those into the loop one line at a time - does 
that help?
Regards
Fred James

#! /bin/bash
#       naming.sh
#       Usage: ./naming.sh directorypath
Dir=$1
for oldName in `ls -1 $Dir`
do
        echo "old name ${oldName}"
        tempName=`echo ${oldName} | gawk -F\_ '{
                for (i = 1;i < NF;i++) {
                        if (length($i) > 1) {
                                printf("%s", toupper(substr($i,1,1)))
                                printf("%s_",substr($i,2))
                        } else {
                                printf("%s_", toupper($i))
                        }
                }
                if (length($i) > 1) {
                        printf("%s", toupper(substr($i,1,1)))
                        printf("%s",substr($i,2))
                } else {
                        printf("%s", toupper($i))
                }
        }'`
        tempName=`echo ${tempName} | sed -e 's/_/ /g'`
        newName=`echo ${tempName} | sed -e 's/ /\ /g'`
        echo "->$oldName<-"
        echo "->$newName<-"
done
exit 0




More information about the PLUG mailing list