Master Linux Text Manipulation with tr and fmt: Practical Examples
Learn how to use the Linux command‑line utilities tr and fmt for powerful text transformation—covering character replacement, case conversion, whitespace handling, and deletion of digits or punctuation, and formatting options—through clear syntax explanations and step‑by‑step examples.
tr Command Overview
The tr utility reads from standard input, translates or deletes characters, and writes the result to standard output.
Syntax
tr [OPTION]... SET1 [SET2]Character Set Notation
SET1 and SET2 may contain:
Literal characters (e.g., a, 5)
Ranges using a hyphen, e.g., a-z Octal escape sequences \NNN (1‑3 digits)
Common escape sequences: \n (newline), \t (tab), \r (carriage return), \b (backspace), \a (bell), \f (form feed), \v (vertical tab)
POSIX character classes inside [: :], e.g., [:lower:], [:upper:], [:digit:], [:space:], [:punct:], [:alnum:], [:blank:], [:cntrl:], [:graph:], [:print:], [:xdigit:] Repetition syntax for SET2: [CHAR*] repeats CHAR to match the length of SET1; [CHAR*REPEAT] repeats CHAR a specific number of times (REPEAT is octal).
Equivalence class [=CHAR=] matches all characters equivalent to CHAR.
Common Options
-d : delete characters in SET1.
-c : complement SET1 (operate on characters *not* in SET1).
-s : squeeze repeated characters in the output.
Typical Use Cases
Replace specific characters : cat file.txt | tr MTS5 ABCD Replaces M, T, S, 5 with A, B, C, D respectively.
Change case : cat file.txt | tr a-z A-Z or cat file.txt | tr [:lower:] [:upper:] Converts lower‑case to upper‑case. The reverse is achieved with tr A-Z a-z or tr [:upper:] [:lower:].
Replace spaces with tabs : cat file.txt | tr [:space:] '\t' Delete characters (using -d): cat file.txt | tr -d 'e' removes all e characters. cat file.txt | tr -d [:punct:] removes punctuation. cat file.txt | tr -d [:digit:] removes digits.
Keep only digits (complement) : cat file.txt | tr -cd [:digit:] outputs only the numeric characters.
fmt Command Overview
The fmt utility reformats plain‑text paragraphs to a specified line width, improving readability.
Syntax
fmt [-WIDTH] [OPTION] [FILE]Key Options
-w N or -WIDTH N : wrap lines at N columns (default 75).
-s : split long lines only at whitespace, preventing word breaks.
-t : prepend a tab to the first line of each paragraph (indentation).
Example Workflow
Create a file file.txt with the following content:
Linux fan www.linuxmi.com shares open source news, tutorials on Linux, programming, big data, operations, and databases.
I was a big brother back then, the webmaster of Linux fanatics, and a Linux enthusiast using the desktop version. I write in my spare time and hope to share some useful tips with Linux beginners and enthusiasts.Apply fmt with different options:
Default formatting (75 columns) : fmt file.txt Custom width (50 columns) : fmt -w 50 file.txt Split only at spaces : fmt -s file.txt Indent first line of each paragraph :
fmt -t file.txtSample Output
Using fmt -w 50 on the example text produces lines wrapped at 50 characters, preserving word boundaries. Using -s ensures no word is broken across lines, while -t adds a leading tab to each paragraph’s first line.
Practical Example Combining tr and fmt
Suppose linuxmi.txt contains: Linuxmi.com. Technology, Simplified! 56789 Typical processing steps:
Convert all lower‑case letters to upper‑case: cat linuxmi.txt | tr a-z A-Z Replace spaces with tabs: cat linuxmi.txt | tr [:space:] '\t' Delete punctuation and keep only alphanumeric characters: cat linuxmi.txt | tr -d [:punct:] | tr -d ' ' Reformat the cleaned text to a width of 40 columns: cat linuxmi.txt | tr -d [:punct:] | fmt -w 40 These pipelines illustrate how tr can be used for character‑level transformations (case conversion, deletion, substitution) and how fmt can subsequently format the resulting text into readable paragraphs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
