fmt命令

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

语法

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

参数

示例

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

cat file.txt
# Hello
# everyone.
# Have
# a
# nice 
# day.

fmt file.txt
# Hello everyone.  Have a nice day.

格式化文件,并使用-w选项指定文件行最大宽度,添加单词超出长度则将单词换行。

cat file.txt
# Hello
# everyone.
# Have
# a
# nice 
# day.

fmt -w 10 file.txt
# Hello
# everyone.
# Have a
# nice day.

-s选项分割了很长的行,但是不重新填充它们。

cat file.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.

fmt -s file.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.

参考

https://www.computerhope.com/unix/ufmt.htm
https://www.runoob.com/linux/linux-comm-fmt.html
https://www.geeksforgeeks.org/fmt-command-unixlinux/