Changing the first letter of every line in a file to uppercase

Go To StackoverFlow.com

7

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.
  • The document contains some special letters: a, a, á, à, ǎ, ā, b, c, d, e, e, é, è, ě, ē, f, g, h, i, i, í, ì, ǐ, ī, j, k, l, m, n, o, o, ó, ò, ǒ, ō, p, q, r, s, t, u, u, ú, ù, ǔ, ü, ǘ, ǜ, ǚ, ǖ, ū, v, w, x, y, and z.
  • The uppercase forms of these letters are: A, A, Á, À, Ǎ, Ā, B, C, D, E, E, É, È, Ě, Ē, F, G, H, I, I, Í, Ì, Ǐ, Ī, J, K, L, M, N, O, O, Ó, Ò, Ǒ, Ō, P, Q, R, S, T, U, U, Ú, Ù, Ǔ, Ü, Ǘ, Ǜ, Ǚ, Ǖ, Ū, V, W, X, Y, and Z.

How can I change the first letter of every line in the file to uppercase?

2012-04-04 07:09
by Village
I think what Engineer meant was what programming language. PHP, etc - Sherwin Flight 2012-04-04 07:13
BASH would be preferred - Village 2012-04-04 07:15
The question is tagged 'bash'.. - The Nail 2012-04-04 07:15
Sorry, didn't notice the 'bash' tag. I removed my PHP answers - Sherwin Flight 2012-04-04 07:18
I am afraid none of the solutions actually work for you, I just realize your character set requirement is in unicode. It means the input file is in a format we don't really know - pizza 2012-04-04 19:14


13

Use sed:

sed  's/^\(.\)/\U\1/' yourfile > convertedfile

Little explanation:

  • the ^ 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. > yourfile) as you will lose your data. If you want to replace in the same file then check out joelparkerhenderson's answer.

2012-04-04 07:14
by The Nail
Works a treat, thank you for a simple effective solution - MitchellK 2018-08-21 11:02


6

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> 
2012-04-04 07:20
by Vijay
Works great on OSX. Thanks - Sufian 2014-05-23 10:50
This was the only one that worked for me on OS - Ben Lindsay 2018-02-18 03:24
sed was giving me hard time on macos. this is cool answe - prayagupd 2018-10-11 23:46


6

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&/'
2012-04-04 12:52
by glenn jackman
I'd say this is the better answer - Thomas Bratt 2013-12-04 12:49


2

To change the file in place:

sed -i -e 's/^\(.\)/\U\1/' file.txt
2012-04-04 07:19
by joelparkerhenderson
Works! But also, Why do I also get the error(warning??) : sed: can't read : No such file or directory - bikashg 2013-06-06 13:46
@bikashg you got the error because there was a typo in my answer-- sorry-- I've fixed it now - joelparkerhenderson 2013-06-09 05:54


1

You can put your special characters in place of a-z and A-Z

function up { local c="$1" ; echo -e "$c" | tr '[a-z]' '[A-Z]' ; }
while read line
do
  echo $(up ${line:0:1})${line:1}
done
2012-04-04 09:57
by pizza


0

Pure bash:

while read x ; do echo "${x^*}" ; done < inputfile > outputfile

Test/demo (remove the code after done for more complete output):

for f in a, a, á, à, ǎ, ā, b, c, d, e, e, é, è, ě, ē, f, g, h, i, i, í, ì, ǐ, ī, \
         j, k, l, m, n, o, o, ó, ò, ǒ, ō, p, q, r, s, t, \
         u, u, ú, ù, ǔ, ü, ǘ, ǜ, ǚ, ǖ, ū, v, w, x, y, and z.
do  echo "$f foo bar." ; done | 
while read x ; do echo "${x^*}" ; done | head -15 | tail -6

Output:

E, foo bar.
E, foo bar.
É, foo bar.
È, foo bar.
Ě, foo bar.
Ē, foo bar.
2017-06-22 13:33
by agc
Ads