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.
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 ALVINAppending the result to a file:
$ echo "Enter department name: " | tr [:lower:] [:upper:] >> deptsReversing 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 >> deptsCase conversion with awk
awkprovides the toupper and tolower functions. Example for uppercase:
$ echo "Enter department name: " | awk '{print toupper($0)}' >> deptsAnd for lowercase:
$ echo "Enter department name: " | awk '{print tolower($0)}' >> deptsCase 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' >> deptsFor lowercase, replace U with L and adjust the character class:
$ echo "Enter department name: " | sed 's/[A-Z]/\L&/g' >> deptsChanging 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- deptsSimilarly with sed:
$ sed 's/[A-Z]/\L&/g' depts > depts-
$ mv depts- deptsCapitalizing 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 \& EngineeringEnsuring 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 \& EngineeringUsing 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 & EngineeringChoose the tool that best fits whether you are processing a single string, a pipeline, or an entire file.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
