Unix Shell - Word capitalization

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2007 > Word capitalization





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Word capitalization
H.K. Kingston-Smith

2007-11-28, 7:34 pm

I have a bunch of files with names containing embedded spaces.
What I would like is to have a shellscript to change the filenames so
that the first letter in each word of a filename is in uppercase, whereas
the remaining letters in the word remain unchanged. Any ideas as to how
to proceed?

Chris F.A. Johnson

2007-11-28, 7:34 pm

On 2007-11-28, H.K. Kingston-Smith wrote:
>
>
> I have a bunch of files with names containing embedded spaces.
> What I would like is to have a shellscript to change the filenames so
> that the first letter in each word of a filename is in uppercase, whereas
> the remaining letters in the word remain unchanged. Any ideas as to how
> to proceed?


_upr()
{
_UPR=
case $1 in
a*) _UPR=A ;; b*) _UPR=B ;;
c*) _UPR=C ;; d*) _UPR=D ;;
e*) _UPR=E ;; f*) _UPR=F ;;
g*) _UPR=G ;; h*) _UPR=H ;;
i*) _UPR=I ;; j*) _UPR=J ;;
k*) _UPR=K ;; l*) _UPR=L ;;
m*) _UPR=M ;; n*) _UPR=N ;;
o*) _UPR=O ;; p*) _UPR=P ;;
q*) _UPR=Q ;; r*) _UPR=R ;;
s*) _UPR=S ;; t*) _UPR=T ;;
u*) _UPR=U ;; v*) _UPR=V ;;
w*) _UPR=W ;; x*) _UPR=X ;;
y*) _UPR=Y ;; z*) _UPR=Z ;;
*) _UPR=${1%${1#?}} ;;
esac
}

_upr "$word"
uword=$_UPR${word#?}


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Ed Morton

2007-11-28, 7:34 pm



On 11/28/2007 3:42 PM, H.K. Kingston-Smith wrote:
> I have a bunch of files with names containing embedded spaces.
> What I would like is to have a shellscript to change the filenames so
> that the first letter in each word of a filename is in uppercase, whereas
> the remaining letters in the word remain unchanged. Any ideas as to how
> to proceed?
>


Assuming that you really mean "the first character in every word, if it is a
letter..." rather than "the first letter in every word...":

awk 'BEGIN{FS=OFS=""}{p=" "; for (i=1;i<=NF;i++) {if (p~/[[:space:]]/) $i=to
upper($i); p=$i} }1' file

Regards,

Ed.

Michael Tosch

2007-11-28, 7:34 pm

Ed Morton wrote:
>
> On 11/28/2007 3:42 PM, H.K. Kingston-Smith wrote:
>
> Assuming that you really mean "the first character in every word, if it is a
> letter..." rather than "the first letter in every word...":
>
> awk 'BEGIN{FS=OFS=""}{p=" "; for (i=1;i<=NF;i++) {if (p~/[[:space:]]/) $i=to
> upper($i); p=$i} }1' file
>
> Regards,
>
> Ed.
>


The following seems to work with any Posix awk:

awk '{for (i=1;i<=NF;i++) {k=substr($i,1,1);
r=substr($i,2); $i=toupper(k) r}}1' file

only that this one replaces whitespace by a single space character.

--
Michael Tosch @ hp : com
Wayne

2007-11-28, 7:34 pm

H.K. Kingston-Smith wrote:
> I have a bunch of files with names containing embedded spaces.
> What I would like is to have a shellscript to change the filenames so
> that the first letter in each word of a filename is in uppercase, whereas
> the remaining letters in the word remain unchanged. Any ideas as to how
> to proceed?
>


Try this PERL script:

#!/usr/bin/perl -Tw
my @words = split(' ', $ARGV[0]);
my $camelCase = shift @words; # First "word" uncapitalized
for (@words) {
s/^(.)(.*)$/\u$1$2/;
$camelCase .= $_;
}
print "$camelCase\n";

---------------------------------------

Running ./camelCase.pl "fee fi fo fum.txt" produces:
feeFiFoFum.txt

This problem will make a good homework assignment!

-Wayne
H.K. Kingston-Smith

2007-11-29, 1:36 am

Thanks everybody for your feedback.



John W. Krahn

2007-11-29, 1:36 am

Wayne wrote:
>
> H.K. Kingston-Smith wrote:
>
> Try this PERL script:
>
> #!/usr/bin/perl -Tw
> my @words = split(' ', $ARGV[0]);
> my $camelCase = shift @words; # First "word" uncapitalized
> for (@words) {
> s/^(.)(.*)$/\u$1$2/;


perldoc perlre
[ SNIP ]
\u uppercase next char (think vi)

So that could be simplified to:

s/^(.+)$/\u$1/;

> $camelCase .= $_;


Or simplified to just:

$camelCase .= ucfirst for @words;

> }
> print "$camelCase\n";


Or why not just:

$ARGV[0] =~ s/([[:alpha:]]+)/\u$1/g;
print "$ARGV[0]\n";



John
--
use Perl;
program
fulfillment
Wayne

2007-11-29, 1:36 am

John W. Krahn wrote:
> Wayne wrote:
>
> Or why not just:
>
> $ARGV[0] =~ s/([[:alpha:]]+)/\u$1/g;
> print "$ARGV[0]\n";
>
> John


Three reasons: I don't know PERL that well, I didn't think of
it, and it doesn't work. But your excellent post inspired me
to produce this:

$ARGV[0] =~ s/ ([^[:space:]]+)/\u$1/g;
print "$ARGV[0]\n";

-Wayne
John W. Krahn

2007-11-29, 1:36 am

Wayne wrote:
>
> John W. Krahn wrote:
>
> Three reasons: I don't know PERL that well, I didn't think of
> it, and it doesn't work.


Can you demonstrate with a simple example of why it doesn't work?

> But your excellent post inspired me
> to produce this:
>
> $ARGV[0] =~ s/ ([^[:space:]]+)/\u$1/g;
> print "$ARGV[0]\n";


In PERL [^[:space:]]+ could be written more simply as \S+.



John
--
use Perl;
program
fulfillment
Stephane Chazelas

2007-11-29, 7:34 am

On Wed, 28 Nov 2007 21:42:58 GMT, H.K. Kingston-Smith wrote:
> I have a bunch of files with names containing embedded spaces.
> What I would like is to have a shellscript to change the filenames so
> that the first letter in each word of a filename is in uppercase, whereas
> the remaining letters in the word remain unchanged. Any ideas as to how
> to proceed?


With zsh:

autoload -U zmv # in ~/.zshrc
zmv -v '(**/)(* *)' '$1${(C)2}'

(C) is zsh's parameter expansion capitalization flag.
(**/) is any level of subdirectories.

If you want to rename the files in the current directory only:

zmv '* *' '${(C)f}'

is enough.

(note that it doesn't process the hidden files (dot files), if
you want to process them as well, you need:
zmv -Q '* *(D)' '${(C)f}'
)
--
Stephane
William James

2007-11-29, 1:29 pm

On Nov 28, 3:42 pm, "H.K. Kingston-Smith" <HK...@yahoo.com> wrote:
> I have a bunch of files with names containing embedded spaces.
> What I would like is to have a shellscript to change the filenames so
> that the first letter in each word of a filename is in uppercase, whereas
> the remaining letters in the word remain unchanged. Any ideas as to how
> to proceed?


If you want a space between each word:

ruby -ne 'puts split.map{|s| s.capitalize}.join(" ")'

If you want the spaces removed:

ruby -ne 'puts split.map{|s| s.capitalize}.join'
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com