简明Linux命令行笔记:fmt

简单的格式化文本

fmt [option] [file-list]

 

fmt通过将所有非空白行的长度设置为几乎相同,来进行简单的文本格式化

 

参数

fmtfile-list中读取文件,并将其内容的格式化版本发送到标准输出。如果不制定文件名或者用连字符(-)来替代文件名,则fmt将从标准输入读取文本信息

 

选项

-s              截断长行,但不合并

-t               除每个段落的第1行外都缩进

-u              改变格式化,使字之间出现一个空格,句子之间出现两个空格

-w n           将输出的行宽改为n个字符。不带该选项时,fmt输出的行宽度为75个字符

 

示例

原文件

$ cat demo 
A long time ago, there was a huge apple tree.         A little boy loved to come and play around it every day. He climbed to the tree top, ate the apples, took a nap under the shadow… He loved the tree and the tree loved to play with him. 

 

fmt -s

$ fmt -s demo 
A long time ago, there was a huge apple tree.         A little boy loved
to come and play around it every day. He climbed to the tree top, ate
the apples, took a nap under the shadow… He loved the tree and the
tree loved to play with him.

截断长行

 

fmt -t

$ fmt -t demo 
A long time ago, there was a huge apple tree.         A little boy loved
   to come and play around it every day. He climbed to the tree top,
   ate the apples, took a nap under the shadow… He loved the tree and
   the tree loved to play with him.

排除首行的缩进

 

fmt -u

$ fmt -u demo 
A long time ago, there was a huge apple tree.  A little boy loved to come
and play around it every day. He climbed to the tree top, ate the apples,
took a nap under the shadow… He loved the tree and the tree loved to
play with him.

格式化单词和句子的间隔,很明显A little boy那里的多个空格被截断到两个

 

fmt -w

$ fmt -w 40 demo 
A long time ago, there was a huge
apple tree.         A little boy
loved to come and play around it
every day. He climbed to the tree top,
ate the apples, took a nap under the
shadow… He loved the tree and the
tree loved to play with him.

指定输出的行宽,这里的行宽为40个字符,我没数过

 

posted @   Nerxious  阅读(1652)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示