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/
相關文章
相關標籤/搜索