Word capitalization
am 28.11.2007 22:42:58 von HKK-S
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?
Re: Word capitalization
am 28.11.2007 23:41:02 von cfajohnson
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
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
Re: Word capitalization
am 29.11.2007 00:00:08 von Ed Morton
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.
Re: Word capitalization
am 29.11.2007 01:14:58 von Michael Tosch
Ed Morton wrote:
>
> 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.
>
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
Re: Word capitalization
am 29.11.2007 01:49:24 von wayne
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
Re: Word capitalization
am 29.11.2007 04:09:22 von HKK-S
Thanks everybody for your feedback.
Re: Word capitalization
am 29.11.2007 04:22:20 von krahnj
Wayne wrote:
>
> 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/;
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
Re: Word capitalization
am 29.11.2007 05:36:56 von wayne
John W. Krahn wrote:
> Wayne wrote:
>> 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?
>> ...
>
> 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
Re: Word capitalization
am 29.11.2007 06:08:05 von krahnj
Wayne wrote:
>
> John W. Krahn wrote:
> > Wayne wrote:
> >> 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?
> >> ...
> >
> > Or why not just:
> >
> > $ARGV[0] =~ s/([[:alpha:]]+)/\u$1/g;
> > print "$ARGV[0]\n";
>
> 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
Re: Word capitalization
am 29.11.2007 08:38:15 von Stephane CHAZELAS
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
Re: Word capitalization
am 29.11.2007 17:52:39 von William James
On Nov 28, 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?
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'