fmt command in Linux with examples - GeeksforGeeks

15 May, 2019
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
$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.
fmt by default with no option used format all the words present in the given file in a single line.
$ cat kt.txt
hello
everyone.
Have
a
nice 
day.

/* fmt by default puts all words 
   in a single line and prints on
   stdout. */
$fmt kt.txt
hello everyone. Have a nice day.

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

/* Here the formatted output gets 
   written in dv.txt */
$fmt kt.txt > dv.txt

-w, – -width=WIDTH option : By default, the maximum width is 75 that fmt command produces in output but with the help of -w option it can be changed, it just requires a numerical value for the width you want to specify.$cat kt.txt
hello everyone. Have a nice day.

/* the width gets reduced to 10 
   with -e option */
$fmt -w 10 kt.txt
hello ever
yone. Have
a nice day.

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

/* the width gets reduced to 10 
   with -e option */
$fmt -w 10 kt.txt
hello ever
yone. Have
a nice day.

-t, – -tagged-paragraph option : 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.

$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 : This option split long lines, but don’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 splited 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.


$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 splited 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 : 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. 

$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 : This option preserves the indentation of the first two lines.
-p, – -prefix=STRING option : This option takes a STRING as an argument and reformat only lines beginning with STRING, reattaching the prefix to reformatted lines.
-g, – -goal=WIDTH option : This option refers to the goal width i.e default of 93% of width.
– -help option : This display a help message and exit.
– -version option : This display version information and exit.
fmt lets you format the large text files easily with the options like -u which can be 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 format the files.
linux-command
Linux-file-commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
scp command in Linux with Examples
Docker - COPY Instruction
mv command in Linux with examples
SED command in Linux | Set 2
chown command in Linux with Examples
nohup Command in Linux with Examples
Named Pipe or FIFO with example C program
Thread functions in C/C++
uniq Command in LINUX with examples
Start/Stop/Restart Services Using Systemctl in Linux