How to format contents of a text file in the Linux system?

LinuxOperating SystemOpen Source

Sometimes our text files are not well format to represent. This work can be done manually for small file, but if the file is too large then manually formatting is so tough work. In such a case to format the contents of a text file in the Linux operating system, we use the fmt command.

The fmt command is used to format, simplifying, and optimizing text files in the Linux system. If the file is not specified then the fmt command reads input from standard input. By default, the fmt command creates text to a width of 75 characters wide.

Syntax

The general syntax of the fmt command is as follows −

fmt [-WIDTH] [OPTION]... [FILE]...

Brief description of options available in the fmt command.

Sr.No.Option & Description
1-c, --crown-margin
Preserve the indentation of the first two lines
2-p, --prefix=STRING
Reformat beginning lines with specified STRING, reattaching the prefix to reformatted lines.
3-s, --split-only
Split only long lines, but don’t refill
4-t, --tagged-paragraph 
Indentation of first-line different from second
5-u, --uniform-spacing
One space between words, two after sentences
6-w, --width=WIDTH 
Maximum line width (default of 75 columns)
7-g, --goal=WIDTH 
Goal width (default of 93% of width)
8--help
Displays a help message and then exits.
9--version
It gives info about the version and then exits.

Here, we will format text from standard input instead of a file in the Linux system using the fmt command as shown below.

$ fmt
Hey,
welcome
to
Tutorialspoint...
Hey, welcome to Tutorialspoint...

Here, we will format text from a file in the Linux system using the fmt command. First, we need to create a file with unformatted text then we will format it using the fmt command. To create a file, we have to use the cat command as shown below.

$ cat >text.txt
Hey,
welcome
to
Tutorialspoint...
^C

Then after, we will format the contents of the file using the fmt command as shown below.

$ fmt text.txt
Hey, welcome to Tutorialspoint...

To check more information about the fmt command, we use the --help option with the fmt command in the Linux operating system as shown below.

$ fmt --help

To check version information of the fmt command, we use the --help option with the fmt command in the Linux operating system as shown below.

$ fmt --help
raja
Published on 01-Jul-2021 14:30:32
Advertisements