将行转换为固定列长度

将行转换为固定列长度

我只想简单地获取给定的文本文件并强制最大列长度为 80。我该如何将其转换为:

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

并将其变成这样:

Four score and seven years ago our fathers brought forth on this continent, a n
ew nation, conceived in Liberty, and dedicated to the proposition that all men 
are created equal.

答案1

使用折叠命令:

fold -s -w 80 filename

-s如果您希望在字符边界而不是单词边界(实际上是空格)处断行,请省略该选项。

我怀疑如果文本采用多字节编码(例如 UTF-8)(并且包含非 ASCII 字符)进行编码,则此方法效果不佳。

如果您使用的是 Windows,请使用赛格威Unx工具类或者操作系统- 它们可能包括折叠。

答案2

$ fmt --width=80 <<< 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.'
Four score and seven years ago our fathers brought forth on this continent,
a new nation, conceived in Liberty, and dedicated to the proposition that
all men are created equal.

相关内容