Tuesday, March 11, 2008

HowTo: Convert First Letter of Text Line to Uppercase

Real quick and easy, I just need to drop it here as I was using it most of the times handling tens of thousand keyword text files.

If you need to convert the first letter of the word or first letter of a text line to its uppercase, read on.

Here's how to convert the first letter of a word from text files.

Say, we have thousands of text lines like
# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~
linux blog
sysad linux blog
systems admin blog
linux blogs
fedora blog
foss
gnu
...

...
gnome
redhat blog
linux blogging
~~~~~~~~~~~~~~~~~~~~~~~~~

and we wish to convert all first letters of each lines to its uppercase letter or capitalized form.

Am going to approach this using one of the powerful command-line drive linux tool called sed. Here's how it can be simply done.

# cat testfile.txt | sed -e 's/\(.\)\(.*\)/\u\1\2/'

Alternatively,

# sed -e 's/\(.\)\(.*\)/\u\1\2/' testfile.txt

which gives you a similar output like so
~~~~~~~~~~~~~~~~~~~~~~~~~
Linux blog
Sysad linux blog
Systems admin blog
Linux blogs
Fedora blog
Foss
Gnu
Gnome
Redhat blog
Linux blogging
~~~~~~~~~~~~~~~~~~~~~~~~~

Hope this helps, enjoy!

Some Articles

Why Choose Fedora over Ubuntu?
Fedora vs RHEL

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy