Make the first character uppercase

Input:

hello
world
monkey

Output should be:

Hello
World
Monkey

How can it be done with perl,sed,awk or bash?

awk -F "" '{$1=toupper($1)}1' OFS= file

Wow, cool...would you please explain a little bit for this code?

sed 's/^./\u&/' file

Hi,

$ perl -ne 'print ucfirst' infile

Regards,
Birei

perl -ne 'print ucfirst($_)' file
awk -F "" '{$1=toupper($1)}1' OFS= file

Explanation:

awk -F ""	# set fieldseparator to "", now a field is one character
$1=toupper($1)	# make the first character uppercase
1		# if the condition is true (1 = true) without an action, awk prints the current line per default
OFS=		# set output field separator to ""

Thank you so much,
It's cool to write AWK code this way, but it's kinda tricky, I guess you are a BIG fan of Perl...:smiley:

It would be more understandable written like this:

Note that the above only works for GNU sed.

My 2 cents:

$ var=abc
$ echo -n ${var:0:1} | tr -s '[:lower:]' '[:upper:]' && echo ${var:1}
Abc

---------- Post updated 02-16-11 at 10:44 AM ---------- Previous update was 02-15-11 at 11:04 PM ----------

My other 2 cents:

$ value=lowercase
$ echo ${value^?}
Lowercase

Search for section "Case modification" on bash's man page.
Seems to work only for bash >= 4.