Linux fmt command

Formatting text

Formatting Text Files with fmt


The "fmt" command can be used to quickly format text and convert it to a different width. By default the fmt command will create text to a width of 75 characters wide. fmt can also be used to apply uniform spacing to a file with the "-u or --uniform-spacing" option. Below are some simple examples of the fmt command in use.



fmt command examples:




Usage: fmt [-WIDTH] [OPTION]... [FILE]...
Reformat each paragraph in the FILE(s), writing to standard output.
The option -WIDTH is an abbreviated form of --width=DIGITS.

Mandatory arguments to long options are mandatory for short options too.
  -c, --crown-margin        preserve indentation of first two lines
  -p, --prefix=STRING       reformat only lines beginning with STRING,
                              reattaching the prefix to reformatted lines
  -s, --split-only          split long lines, but do not refill
  -t, --tagged-paragraph    indentation of first line different from second
  -u, --uniform-spacing     one space between words, two after sentences
  -w, --width=WIDTH         maximum line width (default of 75 columns)
      --help     display this help and exit
      --version  output version information and exit

With no FILE, or when FILE is -, read standard input.

fmt command examples


In the following example I have created a file called test.txt:



john@ls001a:~> cat test.txt

The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

fmt with no options



john@ls001a:~> fmt test.txt
The quick brown fox jumps over the lazy dog.  The quick brown fox
jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.  The quick brown fox
jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.  The quick brown fox
jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

In the above example the lines are joined upto a width of 75 characters.


Specifying a width with the fmt command



john@ls001a:~> fmt --width=20 test.txt
The quick brown fox
jumps over the lazy
dog.  The quick
brown fox jumps
over the lazy dog.
The quick brown fox
jumps over the lazy
dog.  The quick
brown fox jumps
over the lazy dog.
The quick brown fox
jumps over the lazy
dog.  The quick
brown fox jumps
over the lazy dog.
The quick brown fox
jumps over the lazy
dog.  The quick
brown fox jumps
over the lazy dog.
The quick brown fox
jumps over the lazy
dog.  The quick
brown fox jumps
over the lazy dog.

In this example we have set the width to a size of 20 characters.