How to Use the tr and fmt Commands for Text Manipulation on Linux

Linux makes text manipulation and processing very easy with tons of commands. Command-line tools like tr and fmt help you change, format, and modify text files right from your terminal.

In this article, we will discuss how to use the tr and fmt commands to format text in different use cases. So what are tr and fmt? And how do you use them?

4

What Is the tr Command?

The tr command is an abbreviation of the word,translate, and helps a user delete or replace specific characters in a text file. It’s usually used alongside other commands and works by taking standard input, performing an operation on it, and writing the result to standard output.

The tr Command Syntax

The basic syntax of the tr command is:

Sets are strings of characters and some interpreted sequences that the terminal automatically recognizes. Some of them include:

MacBook and a Dell laptop running ZorinOS next to each other

How to Use the tr Command

You can use the tr command to change the cases of a text file, delete numbers, replace spaces with tabs, and even put every word on a new line. The possibilities of the tr command are so vast and there is so much you can achieve if you know how to use it.

For this example, create a text file:muo.txt. The content of the file would be:

firefox logo with yellow warning symbol

Replacing Characters Using the tr Command

To do this, specify the characters you want to replace and what you want to replace them with. For example, if you want to replace the first characters of each word with ABCD, execute this command:

The cat command is used todisplay the contents of a file.

Dell monitor showing Windows 10 desktop

The result:

Change a Text From Lowercase to Uppercase

To do this, execute any of these commands:

Change a Text From Uppercase to Lowercase

Replace Spaces With Tabs

To replace all the spaces with tabs, execute this command:

Alternatively, if you want to replace spaces with the newline character—to print each word on a separate line—replace the “\t” with “\n”.

Ubuntu terminal prompt

Delete Characters Using the tr Command

To delete characters, use the-doption alongside the tr command. For instance, if you want to delete the letter “e” from every word in the muo.txt file, execute this command:

Remove All Punctuation Symbols

If you want to delete all the punctuation symbols in a file without specifying what symbol, use the-doption alongside the interpreted sequence,[:punct:]:

Remove All Digits Using tr

To remove all digits in a text file, execute this command:

Using the tr Complement Option

The tr command comes with the-coption that basically carries out the opposite of the original operation. Let’s use the previous command as an example. If you add the complement option to this command, it would delete all characters that aren’t digits. Take a look:

What Is the fmt Command?

The fmt command is a simple text formatting tool that you could use to print out and process text on Linux. You can also use the command to format email replies.

Toget command-line helpregarding the fmt command, check its manual page by running:

The fmt Command Syntax

The basic syntax of the fmt command is:

How to Use the fmt Command

For this example, create a file calledfile.txtand add the following text:

Using the Default fmt Command

The default fmt command optimizes the text and prints it out in a more readable format. The default width of the fmt command is 75 columns. Execute this command to try out the default fmt operation:

Change the Width of a Text

To change the width of the file, you use the-woption. The syntax looks like this:

To change the file width to 20 columns, execute this command:

Split Long Lines of Text

To split long lines, use the-soption. Try it out:

Indent the First Line of Each Paragraph

To highlight the first line of each paragraph by indenting it, use the-toption. This is the syntax:

Let’s try it out:

tr and fmt: Useful Text Manipulation Commands

tr and fmt make text formatting and processing easy and automated with the different operations they provide. Linux and other Unix-based operating systems offer a large number of text manipulation commands for your every text formatting need. You just need to know what they are and how to use them.

head and tail are two of the most commonly used commands for text manipulation on Linux. Here’s how you could use them to your advantage.

Don’t let aging hardware force you into buying expensive upgrades.

Don’t let someone else take over your phone number.

Obsidian finally feels complete.

Now, I actually finish the books I start.

Quality apps that don’t cost anything.

Technology Explained

PC & Mobile