fmt命令

简介:

简单的格式化文本

fmt [option] [file-list]

 

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

 

参数

fmt从file-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个字符,我没数过

 

 本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/4056992.html,如需转载请自行联系原作者

相关文章
|
5月前
|
Go
go fmt包格式化
go fmt包格式化
32 0
|
5月前
npm ERR! code ERR_STREAM_WRITE_AFTER_END npm ERR! write after en
npm ERR! code ERR_STREAM_WRITE_AFTER_END npm ERR! write after en
|
7月前
|
存储 Linux C语言
深入解析Linux环境下的sprintf()和printf()函数
在C语言中,`sprintf()`和`printf()`函数是用于格式化输出的两个重要函数。`sprintf()`函数将格式化的数据写入一个字符串,而`printf()`函数则将格式化的数据输出到标准输出。在Linux环境中,这两个函数被广泛应用于各种编程任务。本文将详细介绍这两个函数的用法,包括格式化字符串的语法和一些常见的使用场景。
273 1
|
11月前
|
Go 索引
Go 中的格式化字符串`fmt.Sprintf()` 和 `fmt.Printf()`
在 Go 中,可以使用 fmt.Sprintf() 和 fmt.Printf() 函数来格式化字符串,这两个函数类似于 C 语言中的 scanf 和 printf 函数。本文介绍了五个最常用的格式化动词和参数索引的使用方法。
127 0
|
11月前
sprintf用法
sprintf用法
107 0
|
存储 Go
一文了解 Go fmt 标准库输入函数的使用
本篇文章对 fmt 标准库的常用输入函数进行了介绍。输入函数分三类介绍,第一类是从文件获取输入的内容,第二类是从标准输入获取输入的内容,第三类是从字符串里获取输入的内容。并给出简单的示例代码。
89 1
一文了解 Go fmt 标准库输入函数的使用
|
Go
一文了解 Go fmt 标准库输出函数的使用
本篇文章对 fmt 标准库的常用输出函数进行了介绍。输出函数分三类介绍,第一类是输出到输出流里,第二类是标准输出,第三类是输出到字符串里。并给出简单的示例代码。
97 1
一文了解 Go fmt 标准库输出函数的使用
|
Java Linux Go
知识分享之Golang——fmt 打印常用命令整理
知识分享之Golang篇是我在日常使用Golang时学习到的各种各样的知识的记录,将其整理出来以文章的形式分享给大家,来进行共同学习。欢迎大家进行持续关注。 知识分享系列目前包含Java、Golang、Linux、Docker等等。
205 0
知识分享之Golang——fmt 打印常用命令整理
Go string和fmt 详解
今天介绍Go常用的字符串string和数据格式化类fmt
127 0
Go string和fmt 详解