I need a script in any language to capitalize the first letter of every word in a file.
Thanks for all the answers.
Stackoverflow rocks!
Scala:
scala> "hello world" split(" ") map(_.capitalize) mkString(" ")
res0: String = Hello World
or well, given that the input should be a file:
import scala.io.Source
Source.fromFile("filename").getLines.map(_ split(" ") map(_.capitalize) mkString(" "))
Using the non-standard (Gnu extension) sed
utility from the command line:
sed -i '' -r 's/\b(.)/\U\1/g' file.txt
Get rid of the "-i
" if you don't want it to modify the file in-place.
note that you should not use this in portable scripts
A simple perl script that does this: (via http://www.go4expert.com/forums/showthread.php?t=2138)
sub ucwords {
$str = shift;
$str = lc($str);
$str =~ s/\b(\w)/\u$1/g;
return $str;
}
while (<STDIN>) {
print ucwords $_;
}
Then you call it with
perl ucfile.pl < srcfile.txt > outfile.txt
C#:
string foo = "bar baz";
foo = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(foo);
//foo = Bar Baz
zsh solution
#!/bin/zsh
mystring="vb.net lOOKS very unsexy"
echo "${(C)mystring}"
Vb.Net Looks Very Unsexy
Note it does CAP after every non alpha character, see VB.Net
.
In ruby:
str.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase }
hrm, actually this is nicer:
str.each(' ') {|word| puts word.capitalize}