如何在Linux中将每一行输入文本换行以适应指定的宽度?


为了将输入文件中的长行换行以适应指定的宽度,我们使用Linux操作系统中的fold命令。

fold命令用于通过在Linux系统中执行换行操作,使具有长行的文件在有限宽度输出下更易于阅读。大多数Linux/Unix终端的默认屏幕宽度为80列,有时阅读长行文件可能会很麻烦。

fold命令将每行输入文本换行以适应指定的宽度,并允许用户设置行的最大长度。fold命令继承自POSIX的第一个版本。

语法

fold命令的通用语法如下所示。

fold [OPTION]... [FILE]...

fold命令中可用选项的简要说明。

序号选项及说明
1-b, --bytes
按字节计数而不是按列计数
2-c, --characters
按字符计数而不是按列计数
3-s, --spaces
在空格处换行
4-w, --width=WIDTH
使用WIDTH列而不是80列
5--help
显示此帮助信息并退出
6--version
输出版本信息并退出

默认情况下,fold命令在Linux系统中将行换行到80列,如下所示。

vikash@tutorialspoint− ~$ cat text.txt
You can decide what you are going to think in any given situation. Your thoughts
and feelings determine your actions and determine the results you get. It all
starts with your thoughts – and I have found that inspirational words are a quick
way to retune you’re thinking.
vikash@tutorialspoint− ~$ fold text.txt
You can decide what you are going to think in any given situation. Your thoughts
and feelings determine your actions and determine the results you get. It all
starts with your thoughts – and I have found that inspirational words are a quick
way to retune you’re thinking.

要根据指定的宽度换行文件中的行,我们使用-w选项和fold命令,如下所示。

$ cat text.txt
Hey, welcome to tutorialspoint...
$ fold -w3 text.txt
Hey
, w
Elc
Ome
To
Tu
Tor
Spo
int
...

我们也可以使用Linux系统中的fold命令从标准输入而不是文件换行,如下所示。

$ fold -w5
Hey, welcome to Tutorialspoint...
Hey,
Welcome
To
Tutorialspoint...

要检查有关fold命令的更多信息,我们使用--help选项和fold命令在Linux操作系统中,如下所示。

$ fold --help

要检查fold命令的版本信息,我们使用--version选项和fold命令在Linux操作系统中,如下所示。

$ fold --version

更新于:2021年7月1日

616 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.