Linux fmt Command
The Linux fmt command is used to format text files.
The fmt command reads content from the specified file, rearranges it according to the specified format, and outputs it to the standard output device. If the specified file name is "-", the fmt command will read data from the standard input device.
Syntax
fmt [-cstu][-p<column start string>][-w<number of characters per line>][--help][--version][file...]
Parameter Description:
-c or --crown-margin Indents the first two lines of each paragraph.
-p<column start string> or -prefix=<column start string> Only combines lines containing the specified string, typically used for comments in programming languages.
-s or --split-only Splits lines that exceed the number of characters per line but does not combine lines that are shorter.
-t or --tagged-paragraph Indents the first two lines of each paragraph, but with a different format for the first and second lines.
-u or --uniform-spacing Spaces between characters are one space character, and spaces between sentences are two space characters.
-w<number of characters per line> or --width=<number of characters per line> or -<number of characters per line> Sets the maximum number of characters per line.
--help Online help.
--version Displays version information.
Example
Rearrange the specified file. For example, if the file testfile contains 5 lines of text, you can rearrange the format of the file with the command:
fmt testfile
The output is as follows:
$ fmt testfile # Rearrange the testfile file
hello Linux! Linux is a free Unix-type operating system. This is a
Linux testfile! Linux Linux
To rearrange the file testfile into 85 characters per line and output it to the standard output device, the command should be:
fmt -w 85 testfile
For comparison, first use the cat command to view the file content:
$ cat testfile # View the content of the testfile file
hello Linux!
Linux is a free Unix-type operating system.
This is a Linux testfile!
Linux
Linux
After using the fmt command to rearrange, the output is as follows:
$ fmt -w 85 testfile # Specify the rearrangement width as 85 characters
hello Linux! Linux is a free Unix-type operating system. This is a Linux testfile!
Linux Linux