Bash text editing problem

From: Foptop (youknow_at_idont.com)
Date: 03/04/04


Date: Thu, 04 Mar 2004 19:05:34 GMT

don't know if anyone remembers, but i asked the newsgroup of a way to
convert the first letter of each word in a filename to uppercase. the
conditions were: it had to be bash ( haven't learnt any other shell
languages yet ) ; words were separated with an underscore ; cases of
letters could be anything. a solution was quickly posted. i think the
guy's name was Dan, could be wrong but i think that's it. here is his
function:

#!/bin/bash
#

function upperCase
{
filename=$(IFS=_
           set -- $1
           for i
           do
                remain=${i#?}
                firstChar=${i%${remain}}
                upper=$(echo $firstChar|tr [:lower:] [:upper:])
                echo -e "${upper}${remain}_\c"
           done
          )
echo ${filename%_}
}

upperCase $1

i'm just posting this again since it works perfectly and the coding is
nice and neat - someone might want it as i could not find a proper
solution to this. i saw a solution in perl, but didn't understand it, so
couldn't adapt it. it only goes one way though - if everything is in
uppercase, it won't change the other characters to lowercase. it works
when everything is lowercase or if the only capitals are in the first
letter of some/all of the words. this can all be changed easily though,
but the problem i couldn't figure out was how to split up a word and deal
with the first character and the rest of the word separately. so far, i've
made a few scripts based on this function to automate a lot of tasks.
thanks again to Dan.



Relevant Pages

  • Changing Uppercase filenames into Lowercase
    ... I already found MAKEUP.COM which breaks down a filename ... uses the RENAME command to rename lowercase names and ... lowercase filename. ... The filename remains uppercase because DCL uppercases ...
    (comp.os.vms)
  • Re: Changing Uppercase filenames into Lowercase
    ... > I already found MAKEUP.COM which breaks down a filename ... > uses the RENAME command to rename lowercase names and ... > lowercase filename. ... > The filename remains uppercase because DCL uppercases ...
    (comp.os.vms)
  • Re: strtolower wont do his job
    ... > well do that before you store it. ... To convert the first character to uppercase you can use ucfirst. ... lowercase character. ...
    (alt.php)
  • Re: strtolower wont do his job
    ... > well do that before you store it. ... To convert the first character to uppercase you can use ucfirst. ... lowercase character. ...
    (comp.lang.php)
  • Re: Bash text editing problem
    ... > convert the first letter of each word in a filename to uppercase. ... > guy's name was Dan, could be wrong but i think that's it. ... it won't change the other characters to lowercase. ... > with the first character and the rest of the word separately. ...
    (comp.os.linux)