fmt命令

fmt命令

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

语法

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

参数

  • -c, --crown-margin: 保留前两行的缩进。
  • -p, --prefix=STRING: 仅从新格式化以STRING开头的行,并将前缀从新附加到格式化后的行上。
  • -s, --split-only: 拆分长行,但再也不从新填充它们。
  • -t, --tagged-paragraph: 第一行的缩进不一样于第二行。
  • -u, --uniform-spacing: 单词之间使用一个空格,句子后面使用两个空格。
  • -w, --width=WIDTH: 最大行宽(默认为75列)。
  • -g, --goal=WIDTH: 目标宽度(默认为宽度的93%)。
  • --help: 输出帮助信息。
  • --version: 输出版本信息。

示例

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

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

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

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

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

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

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

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://github.com/WindrunnerMax/EveryDay

参考

https://www.computerhope.com/unix/ufmt.htm
https://www.runoob.com/linux/linux-comm-fmt.html
https://www.geeksforgeeks.org/fmt-command-unixlinux/
相关文章
相关标签/搜索