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

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

by Staff
0 comment

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?


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:

 tr [option]... SET1 [SET2]

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

\           Backslash
b Backspace
t Horizontal tab
v Vertical tab
[:alnum:] All letters and digits
[:alpha:] All letters
[:blank:] Horizontal whitespaces
[:digit:] All digits
[:lower:] All lowercase characters
[:punct:] All punctuation characters
[:space:] Horizontal or vertical whitespace characters
[:upper:] All uppercase letters

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:

MakeUseOf. Technology, Simplified! 1234   

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:

cat muo.txt | tr MTS1 ABCD

The result:

AakeUseOf. Bechnology, Cimplified! D234

Change a Text From Lowercase to Uppercase

To do this, execute any of these commands:

cat muo.txt | tr a-z A-Z   
cat muo.txt | tr [:lower:] [:upper:]

The result:

MAKEUSEOF. TECHNOLOGY, SIMPLIFIED! 1234   

Change a Text From Uppercase to Lowercase

To do this, execute any of these commands:

cat muo.txt | tr A-Z a-z   
cat muo.txt | tr [:upper:] [:lower:]

The result:

makeuseof. technology, simplified! 1234

Replace Spaces With Tabs

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

cat muo.txt | tr [:space:] 't' 

The result:

MakeUseOf.      Technology,     Simplified!     1234      

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

Delete Characters Using the tr Command

To delete characters, use the -d option 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:

cat muo.txt | tr -d 'e'      

The result:

MakUsOf. Tchnology, Simplifid! 1234 

Remove All Punctuation Symbols

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

cat muo.txt | tr -d [:punct:]

The result:

MakeUseOf Technology Simplified 1234

Remove All Digits Using tr

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

cat muo.txt | tr -d [:digit:] 

The result:

MakeUseOf. Technology, Simplified!          

Using the tr Complement Option

The tr command comes with the -c option 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:

cat muo.txt | tr -cd [:digit:] 

The result:

1234   

What Is the fmt Command?

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

To get command-line help regarding the fmt command, check its manual page by running:

man fmt

The fmt Command Syntax

The basic syntax of the fmt command is:

fmt [-WIDTH] [OPTION] [FILE]

How to Use the fmt Command

For this example, create a file called file.txt and add the following text:

Technology, Simplified
We're tech enthusiasts on a mission to teach the world how to use and understand the tech in their lives.

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:

fmt file.txt

The result:

Technology, Simplified We're tech enthusiasts on a mission to teach the
world how to use and understand the tech in their lives.

Change the Width of a Text

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

fmt -w N filename

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

fmt -w 20 file.txt

The result:

Technology,
Simplified We're
tech enthusiasts on
a mission to teach
the world how to
use and understand
the tech in their
lives.

Split Long Lines of Text

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

fmt -s file.txt

The result:

Technology, Simplified
We're tech enthusiasts on a mission to teach the world how to use and
understand the tech in their lives.

Indent the First Line of Each Paragraph

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

fmt -t filename.

Let’s try it out:

fmt -t file.txt

The result:

Technology, Simplified
We're tech enthusiasts on a mission to teach the world how to use and
understand the tech in their lives.

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.



Read the full article here

You may also like

Leave a Comment

Iman Hearts is one of the biggest lifestyle news and articles portals, we provide the latest news and articles about family, lifestyle, entertainment, and many more, follow us to get the latest news about what matters to you.

 

© 2022 Iman Hearts. All rights reserved. Sitemap