Linux fmt命令

Linux 命令大全

fmt命令用于编排文本文件,其会从指定的文件里读取内容,将其依照指定格式重新编排后,输出到标准输出设备,若指定的文件名为-,则fmt指令会从标准输入设备读取数据。

语法

fmt [-WIDTH] [OPTION]... [FILE]...

参数

实例

linux中通过cat命令查看文件的内容

[root@iz8vbbxoyfcgyzkxcspg04z mnt]# cat test.txt 
Reformat
each
paragraph
in
the
FILE(s),
writing
to
standard
output.
The
option
-WIDTH
is
an
abbreviated
form
of
--width=DIGITS.
Reformat each paragraph in the FILE(s), writing to standard output.  The option -WIDTH is an abbreviated form of --width=DIGITS.

fmt不使用任何选项,默认情况下,将给定文件中存在的所有单词格式化为一行,当然默认单行最大宽度75。

[root@iz8vbbxoyfcgyzkxcspg04z mnt]# fmt test.txt #重排test.txt内容
Reformat each paragraph in the FILE(s), writing to standard output.
The option -WIDTH is an abbreviated form of --width=DIGITS.  Reformat each
paragraph in the FILE(s), writing to standard output.  The option -WIDTH
is an abbreviated form of --width=DIGITS.

fmt -s  拆分长行,但不再重新填充它们

[root@iz8vbbxoyfcgyzkxcspg04z mnt]# fmt -s test.txt 
Reformat
each
paragraph
in
the
FILE(s),
writing
to
standard
output.
The
option
-WIDTH
is
an
abbreviated
form
of
--width=DIGITS.
Reformat each paragraph in the FILE(s), writing to standard output.
The option -WIDTH is an abbreviated form of --width=DIGITS.

fmt -t 第一行的缩进不同于第二行

[root@iz8vbbxoyfcgyzkxcspg04z mnt]# fmt -t test.txt 
Reformat
each
paragraph
in
the
FILE(s),
writing
to
standard
output.
The
option
-WIDTH
is
an
abbreviated
form
of
--width=DIGITS.
Reformat each paragraph in the FILE(s), writing to standard output.
   The option -WIDTH is an abbreviated form of --width=DIGITS. #此地方有缩进

fmt -u 单词之间使用一个空格,句子后面使用两个空格

[root@iz8vbbxoyfcgyzkxcspg04z mnt]# fmt -u test.txt 
Reformat each paragraph in the FILE(s), writing to standard output.
The option -WIDTH is an abbreviated form of --width=DIGITS.  Reformat each
paragraph in the FILE(s), writing to standard output.  The option -WIDTH
is an abbreviated form of --width=DIGITS.

fmt -w 设置最大行宽(默认为75列)

[root@iz8vbbxoyfcgyzkxcspg04z mnt]# fmt -w 30 test.txt 
Reformat each paragraph
in the FILE(s), writing to
standard output.  The option
-WIDTH is an abbreviated
form of --width=DIGITS.
Reformat each paragraph
in the FILE(s), writing to
standard output.  The option
-WIDTH is an abbreviated form
of --width=DIGITS.

fmt -g 目标宽度(默认为宽度的93%)

[root@iz8vbbxoyfcgyzkxcspg04z mnt]# fmt -g 30 test.txt 
Reformat each paragraph in the
FILE(s), writing to standard
output.  The option -WIDTH is an
abbreviated form of --width=DIGITS.
Reformat each paragraph in the
FILE(s), writing to standard
output.  The option -WIDTH is
an abbreviated form of --width=DIGITS.

Linux 命令大全