views:

2650

answers:

13

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!

+2  A: 

php uses ucwords($string) or ucwords('all of this will start with capitals') to do the trick. so you can just open up a file and get the data and then use this function:

<?php 
$file = "test.txt"; 
$data = fopen($file, 'r');
$allData = fread($data, filesize($file));
fclose($fh);
echo ucwords($allData);
?>

Edit, my code got cut off. Sorry.

stogdilla
Can be shortened to: <?php file_put_contents("test.txt", ucwords(file_get_contents("test.txt")));
too much php
+1  A: 

This is done in PHP.

$string = "I need a script in any language to capitalize the first letter of every word in a file."
$cap = ucwords($string);
rymn
+2  A: 

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
MiffTheFox
+7  A: 

In Python, open('file.txt').read().title() should suffice.

Alex Martelli
that's neat! go python
Bayard Randel
Wow, that's way better than my Python script. Nice!
htw
Does it work for non-ASCII characters? E.g. European accented characters?
Craig McQueen
@Craig: on regular strings it's "locale dependent," so for safety convert to unicode: unicode(open("file.txt").read(), "encoding").title()
Neil Williams
What you say is astonishing, but a note of warning: the OP asks to capitalize the first letter without probably touching the others. title() capitalizes the first but puts lowercase all the others: "heLLO".title() returns 'Hello'.
Stefano Borini
A: 

In ruby:

str.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase }

hrm, actually this is nicer:

str.each(' ') {|word| puts word.capitalize}
Bayard Randel
irb> foo = "foo bar"; foo.gsub(/\b(\w)/) { |a| a.upcase } => "Foo Bar"
jess
+1  A: 

ruby:

irb> foo = ""; "foo bar".split.each { |x| foo += x.capitalize + " " }

=> ["foo", "bar"]

irb> foo

=> "Foo Bar "

jess
that probably doesn't work because it loses all the formatting. turns any sequence of multiple spaces or tabs into just one space.
glenra
True. the gsub solution from ruby is better than this. I was just trying to be creative. :)
jess
A: 

perl:

$ perl -e '$foo = "foo bar"; $foo =~ s/\b(\w)/uc($1)/ge; print $foo;'

Foo Bar

jess
thank you for reminding me why I don't use perl :)
Bayard Randel
just to prove that it's not perl that's ugly, I gave you this exact solution in ruby as a response to your solution. :P
jess
I'll concede that regex is universally gross :)
Bayard Randel
+2  A: 

From the shell, using ruby, this works assuming your input file is called FILENAME, and it should preserve all existing file formatting - it doesn't collapse the spacing as some other solutions might:

cat FILENAME | ruby -n -e 'puts $_.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase }'
glenra
A: 

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(" "))
andri
+4  A: 

C#:

string foo = "bar baz";
foo = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(foo);
//foo = Bar Baz
Dan Walker
+1  A: 
VB.Net:

Dim sr As System.IO.StreamReader = New System.IO.StreamReader("c:\lowercase.txt")
Dim str As String = sr.ReadToEnd()
sr.Close()
str = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str)
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("c:\TitleCase.txt")
sw.Write(str)
sw.Close()
James Lawruk
+1, but in vb.net you could use "StrConv" function. Example: StrConv("string goes here", VbStrConv.ProperCase)
Cleiton
+1  A: 

Here's another Ruby solution, using Ruby's nice little one-line scripting helpers (automatic reading of input files etc.)

ruby -ni~ -e "puts $_.gsub(/\b\w+\b/) { |word| word.capitalize }" foo.txt

(Assuming your text is stored in a file named foo.txt.)

Best used with Ruby 1.9 and its awesome multi-language support if your text contains non-ASCII characters.

Jörg W Mittag
+3  A: 

Using the standard (Gnu) 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.