Linux fmt Command
The Linux fmt command is used to format text files.
The fmt command reads content from a specified file, rearranges it according to the specified format, and writes it to standard output. If the specified file name is "-", fmt reads data from standard input.
Syntax
fmt [-cstu][-p<列起始字符串>][-w<每列字符数>][--help][--version][文件...]
Parameter Description:
- -c or --crown-margin indent the first two columns of each paragraph.
- -p
or -prefix= only join lines that contain the specified string, often used for comments in programming languages. - -s or --split-only only split lines that exceed the specified width, but do not join lines shorter than it.
- -t or --tagged-paragraph indent the first two columns of each paragraph, but with different indentation for the first and second lines.
- -u or --uniform-spacing separate every word by one space and every sentence by two spaces.
- -w
or --width= or - set the maximum number of characters per line. - --help Online help.
- --version Display version information.
Example
Reformat the specified file. If the file testfile has 5 lines of text, you can reformat it with the following command:
fmt testfile
The output is as follows:
$ fmt testfile #重排testfile 文件
hello Linux! Linux is a free Unix-type operating system. This is a
Linux testfile! Linux Linux
To reformat testfile to 85 characters per line and output it to standard output, the command should be:
fmt -w 85 testfile
For comparison, first use the cat command to view the file contents:
$ cat testfile #查看testfile 文件的内容
hello Linux!
Linux is a free Unix-type operating system.
This is a Linux testfile!
Linux
Linux
After reformatting with the fmt command, the output is as follows:
$ fmt -w 85 testfile #指定重排宽度为85个字符
hello Linux! Linux is a free Unix-type operating system. This is a Linux testfile!
Linux Linux
Linux Command Reference