Linux fmt命令

Linux 命令大全 Linux 命令大全

Linux fmt命令用于編排文本文件。

fmt指令會(huì)從指定的文件里讀取內(nèi)容,將其依照指定格式重新編排后,輸出到標(biāo)準(zhǔn)輸出設(shè)備。若指定的文件名為"-",則fmt指令會(huì)從標(biāo)準(zhǔn)輸入設(shè)備讀取數(shù)據(jù)。

語(yǔ)法

fmt [-cstu][-p<列起始字符串>][-w<每列字符數(shù)>][--help][--version][文件...]

參數(shù)說(shuō)明

  • -c或--crown-margin 每段前兩列縮排。
  • -p<列起始字符串>或-prefix=<列起始字符串> 僅合并含有指定字符串的列,通常運(yùn)用在程序語(yǔ)言的注解方面。
  • -s或--split-only 只拆開(kāi)字?jǐn)?shù)超出每列字符數(shù)的列,但不合并字?jǐn)?shù)不足每列字符數(shù)的列。
  • -t或--tagged-paragraph 每列前兩列縮排,但第1列和第2列的縮排格式不同。
  • -u或--uniform-spacing 每個(gè)字符之間都以一個(gè)空格字符間隔,每個(gè)句子之間則兩個(gè)空格字符分隔。
  • -w<每列字符數(shù)>或--width=<每列字符數(shù)>或-<每列字符數(shù)> 設(shè)置每列的最大字符數(shù)。
  • --help 在線(xiàn)幫助。
  • --version 顯示版本信息。

實(shí)例

重排指定文件。如文件testfile共5 行文字,可以通過(guò)命令對(duì)該文件格式進(jìn)行重排,其命令為:

fmt testfile 

輸出結(jié)果如下:

$ fmt testfile #重排testfile 文件  
hello Linux! Linux is a free Unix-type operating system. This is a  
Linux testfile! Linux Linux 

將文件testfile重新排成85 個(gè)字符一行,并在標(biāo)準(zhǔn)輸出設(shè)備上輸出,其命令應(yīng)該為:

fmt -w 85 testfile

為了對(duì)比,先使用cat 命令查看文件內(nèi)容:

$ cat testfile #查看testfile 文件的內(nèi)容  
hello Linux!  
Linux is a free Unix-type operating system.  
This is a Linux testfile!  
Linux  
Linux 

使用fmt命令重排之后,輸出結(jié)果如下:

$ fmt -w 85 testfile #指定重排寬度為85個(gè)字符  
hello Linux! Linux is a free Unix-type operating system. This is a Linux testfile!  
Linux Linux 

Linux 命令大全 Linux 命令大全