Open In App

fmt command in Linux with examples

Last Updated : 05 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here’s the syntax of fmt command:

syntax of `fmt` command in Linux

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

where, the -WIDTH is an abbreviated firm of –width=DIGITS and OPTION refers to the options compatible with the fmt command and FILE refers to the file name. If no FILE is specified, or if FILE is a dash(“-“), fmt reads from the standard input.

Using fmt command

fmt by default with no option used format all the words present in the given file in a single line.

cat kt.txt

Used `cat` command to view the content in this file name `kt.txt`

fmt kt.txt

Used `fmt` command to view the content in single line.

fmt kt.txt

fmt kt.txt

To save or write the formatted output you can use fmt as:

fmt kt.txt > dv.txt

Here we have use `>` to save the output in filename `dv.txt`

fmt kt.txt > dv.txt” srcset=”https://media.geeksforgeeks.org/wp-content/uploads/20230623105518/319.png 600w, https://media.geeksforgeeks.org/wp-content/uploads/20230623105518/319-100.png 100w, https://media.geeksforgeeks.org/wp-content/uploads/20230623105518/319-200.png 200w, https://media.geeksforgeeks.org/wp-content/uploads/20230623105518/319-300.png 300w, ” sizes=”100vw” width=”600″><figcaption>fmt kt.txt > dv.txt</figcaption></figure>
<h2>Options for fmt command</h2>
<h3><strong>`-w, – -width=WIDTH` option in `fmt` command in Linux</strong></h3>
<p>By default, the maximum width is 75 that fmt command produces in output but with the help of <strong>-w</strong> option it can be changed, it just requires a numerical value for the width you want to specify.</p>
<pre>fmt -w 10 dv.txt</pre>
<p>Replace your desired filename `dv.txt`</p>
<div style=fmt -w 10 dv.txt

fmt -w 10 dv.txt

`-t, – -tagged-paragraph` option in `fmt` command in Linux 

There can be a need for highlighting the very first line in a text file which can be done by making the indentation of first line different from the other lines which can be done with -t command.

$cat kt.txt
hello everyone. Have a nice 
and prosperous day.

/*-t makes the indentation
   of first line different
   from others */
$fmt -t kt.txt
hello everyone. Have a nice
   and prosperous day.

`-s` option in“fmt` command in Linux

This option splits long lines, but doesn’t refill them.

$cat kt.txt
Love is patient, love is kind. It does not envy,
 it does not boast, it is not proud. It is not rude,
 it is not self-seeking, it is not easily angered, 
it keeps no record of wrongs. Love does not delight 
in evil but rejoices with the truth. It always protects,
 always trusts, always hopes, always perseveres. 
Love never fails.


/* long lines get splitted with -s option */
$fmt -s kt.txt
Love is patient, love is kind.
It does not envy, it does not boast, it is not proud.
It is not rude, it is not self-seeking, 
it is not easily angered, it keeps no record of wrongs.
Love does not delight in evil but rejoices with the truth.
It always protects, always trusts, always hopes, always perseveres. 
Love never fails.

`-u, – -uniform-spacing` option in `fmt` command in Linux

This option uses one space between words and two spaces after sentences for formatting.

$cat kt.txt
Love   is   patient,   love is   kind.
    It does   not envy, it   does not boast,
 it is not   proud. 

/* Spaces are uniformed with -u option */
$fmt -u kt.txt
Love is patient, love is kind.  It does not envy,
it does not boast, it is not proud. 

`-c, – -crown-margin` option in `fmt` command in Linux

This option preserves the indentation of the first two lines.

-p, – -prefix=STRING option in `fmt` command in Linux

This option takes STRING as an argument and reformat only lines beginning with STRING, reattaching the prefix to reformatted lines.

-g, – -goal=WIDTH option in `fmt` command in Linux

This option refers to the goal width i.e default of 93% of width.

`- -help` option in `fmt` command in Linux

This displays a help message and exit.

fmt --help
fmt --help

fmt –help

`- -version` option in `fmt` command in Linux

 This displays version information and exit.

fmt --version
fmt --version

fmt –version

Application of fmt command in Linux

  • fmt lets you format the large text files easily with the options like -u which can be a very difficult task if done manually.
  • fmt also lets you change the default width with the help of -w option.
  • It is a good way to save time when it comes to formatting the files.

Conclusion

In this article we have discussed `fmt` command in Linux, which provides a convenient way to format text file effectively. We discussed that it can be used for splitting the long lines, enforce uniform spacing, handle tagged paragraph, or customize the width. Overall, we can say that after understanding this article we can enhance the readability and appearance of our text files, saving time and efforts in the process 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads