Fundamentals 6 min read

Master Linux Case Conversion with tr, awk, sed, and Python

This guide walks you through Linux command‑line tools—tr, awk, sed, and even Python—to efficiently convert text case, handle whole‑file transformations, capitalize only first letters, and ensure proper title‑case formatting for scripts and shell pipelines.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Case Conversion with tr, awk, sed, and Python

Using tr for case conversion

The tr (translate) command is one of the simplest ways to change case on the command line or in scripts. To convert a string to uppercase:

$ echo hello alvin | tr [:lower:] [:upper:]
HELLO ALVIN

Appending the result to a file:

$ echo "Enter department name: " | tr [:lower:] [:upper:] >> depts

Reversing the order ( [:upper:] [:lower:]) produces lowercase output.

You can also use character ranges ( a-z and A-Z) as shortcuts for the POSIX classes.

$ echo "Enter department name: " | tr a-z A-Z >> depts

Case conversion with awk

awk

provides the toupper and tolower functions. Example for uppercase:

$ echo "Enter department name: " | awk '{print toupper($0)}' >> depts

And for lowercase:

$ echo "Enter department name: " | awk '{print tolower($0)}' >> depts

Case conversion with sed

The stream editor sed can perform similar transformations using regular‑expression replacements. Uppercase conversion:

$ echo "Enter department name: " | sed 's/[a-z]/\U&/g' >> depts

For lowercase, replace U with L and adjust the character class:

$ echo "Enter department name: " | sed 's/[A-Z]/\L&/g' >> depts

Changing the case of an entire file

Both awk and sed can read a file and output the transformed text. To display a file in lowercase without modifying it: $ awk '{print tolower($0)}' depts To actually rewrite the file, redirect the output to a temporary file and rename it:

$ awk '{print tolower($0)}' depts > depts-
$ mv depts- depts

Similarly with sed:

$ sed 's/[A-Z]/\L&/g' depts > depts-
$ mv depts- depts

Capitalizing only the first letter of each word

Use sed with a word‑boundary pattern:

$ echo design \& engineering | sed -e "s/\b\(.\)/\u\1/g"
Design \& Engineering

Ensuring proper title case (first letter uppercase, rest lowercase)

For more complex name lists, a longer sed expression can enforce title case:

$ echo design \& ENGINEERING | sed 's/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g'
Design \& Engineering

Using Python for title‑case conversion

If Python is available, a one‑liner can be clearer than the sed regex:

$ echo -n "design & engineering" | python3 -c "import sys; print(sys.stdin.read().title())"
Design & Engineering

Choose the tool that best fits whether you are processing a single string, a pipeline, or an entire file.

Linuxshell scriptingawksedtrcase-conversion
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.