I need to change the first letter of every line in a file to uppercase, e.g.
the bear ate the fish.
the river was too fast.
Would become:
The bear ate the fish.
The river was too fast.
How can I change the first letter of every line in the file to uppercase?
Use sed
:
sed 's/^\(.\)/\U\1/' yourfile > convertedfile
Little explanation:
^
represents the start of a line..
matches any character\U
converts to uppercase\( ... \)
specifies a section to be referenced later (as \1
in this case); parentheses are to be escaped here.Do not try to redirect the output to the same file in one command (i.e. ) as you will lose your data. If you want to replace in the same file then check out joelparkerhenderson's answer.> yourfile
pearl.311> cat file1
linenumber11
linenumber2
linenumber1
linenumber4
linenumber6
pearl.312> awk '{print toupper(substr($0,1,1))""substr($0,2)}' file1
Linenumber11
Linenumber2
Linenumber1
Linenumber4
Linenumber6
pearl.313>
There's a few sed answers with s/^\(.\)/\U\1/
. GNU sed also has a \u
directive that changes only the next letter to uppercase, so
sed 's/./\u&/'
Although if the first character on a line is a space, you won't see an uppercase letter, so
sed 's/[[:alpha:]]/\u&/'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With