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

Village picture Village · Apr 4, 2012 · Viewed 10.5k times · Source

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?

Answer

The Nail picture The Nail · Apr 4, 2012

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.