linux简单文本格式化是什么-fmt命令使用与实例

linux简单文本格式化是什么-fmt命令使用与实例

linux fmt命令

Linux系统中的fmt命令主要用于文本内容的格式调整。

该命令会从给定的文件中读取内容,按照指定的格式重新整理后输出到标准输出。如果提供的文件名是”-“,则表示从标准输入获取数据进行处理。

命令语法

<pre class="brush:php;toolbar:false">fmt [-cstu][-p][-w][--help][--version][文件...]

参数说明

  • -c或–crown-margin 段落开头两列缩进。
  • -p或–prefix= 仅合并包含特定字符串的行,常用于代码注释排版。
  • -s或–split-only 只对超出设定列宽的行进行拆分,不合并未满列宽的行。
  • -t或–tagged-paragraph 行首缩进两列,但第一列和第二列缩进方式不同。
  • -u或–uniform-spacing 所有字符之间用单个空格分隔,句子之间使用两个空格。
  • -w或–width=或- 设置每行的最大字符数。
  • –help 显示帮助信息。
  • –version 查看当前版本号。

使用示例

对testfile文件进行格式重排操作。假设该文件包含5行内容,可以执行以下命令:

<pre class="brush:php;toolbar:false">fmt testfile 

输出效果如下:

<pre class="brush:php;toolbar:false">$ fmt testfile #对testfile 文件进行重排   hello Linux! Linux is a free Unix-type operating system. This is a   Linux testfile! Linux Linux 

若希望将testfile文件按每行最多85字符进行重排,并显示在终端上,可使用:

<pre class="brush:php;toolbar:false">fmt -w 85 testfile

首先查看原文件内容:

<pre class="brush:php;toolbar:false">$ cat testfile #查看testfile 文件的内容   hello Linux!   Linux is a free Unix-type operating system.   This is a Linux testfile!   Linux   Linux 

执行格式化后结果为:

<pre class="brush:php;toolbar:false">$ fmt -w 85 testfile #设置每行最大宽度为85字符   hello Linux! Linux is a free Unix-type operating system. This is a Linux testfile!   Linux Linux 

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享