Thursday, January 3, 2013

Caplitalize(change to uppercase )first letter of each word in a line

How can you capitalize each word in a line using perl?

let say there is a file:
> cat temp
hi this is world


how can we make it
Hi This Is World


its very simple to do it in perl:

perl -pe '$_=~s/\b(\w)/\U$1/g;' your_file


if you want to do it inplace just add  a -i flag:

perl -pi -e '$_=~s/\b(\w)/\U$1/g;' your_file

No comments:

Post a Comment