fmt Command Examples in Linux

This tutorial explains Linux “fmt” command, options and its usage with examples.

fmt – Simple text formatter.

DESCRIPTION

fmt is a formatter for simplifying and optimizing text files.

SYNOPSIS

fmt [-c] [-s] [-w width | -width ] [inputfile]

OPTIONS

advertisement
advertisement

-c
Crown margin mode. Preserve the indentation of the first two lines within a paragraph, and align the left margin of each subsequent line with that of the second line. This is useful for tagged paragraphs.
-s
Split lines only. Do not join short lines to form longer ones. This prevents sample lines of code, and other such formatted text, from being unduly combined.
-w width | -width
Fill output lines to up to width columns.
inputfile
The input file.

EXAMPLES

1. Given the contents of file file1.txt, fmt works as follows

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ cat file1.txt
abc
def
ghi
$ fmt file1.txt
abc def ghi

Simply using fmt formats text in a single line.

2. Let abc file contains some text, then giving a width of 1 produces output

$ cat abc
  This is a brief message to the Control Unit group.
     The new hp is now on-line. To send prints to it, type lj filename.
     If you have questions contact Sys. Adm. (x480).  The printer is
     located in the stock room.
$ fmt -w 1  abc
  This
  is
  a
  brief
  message
  to
  the
  Control
  Unit
  group.
     The
     new
     hp
     is
     now
     on-line.
     To
     send
     prints
     to
     it,
     type
     lj
     filename.
     If
     you
     have
     questions
     contact
     Sys.
     Adm.
     (x480).
     The
     printer
     is
     located
     in
     the
     stock
     room.

3. Crown Margin Mode

advertisement

This example shows the crown margin usage.

$ cat abc
  This is a brief message to the Control Unit group.
     The new hp is now on-line. To send prints to it, type lj filename.
     If you have questions contact Sys. Adm. (x480).
     The printer is located in the stock room.
$ fmt -c abc
  This is a brief message to the Control Unit group.  The new hp is
     now on-line. To send prints to it, type lj filename.  If you have
     questions contact Sys. Adm. (x480).  The printer is located in the
     stock room.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

advertisement
If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.