使用 bash 缩小文件中的文本

0 bash text-processing

我的文本文件如下所示:

If you are a software developer in your 20s or 30s, you've grown up in a world dominated by Linux. It has been a significant player in the data center for decades, and while it's hard to find definitive operating system market share reports, Linux's share of data center operating systems could be as high as 70%, with Windows variants carrying nearly all the remaining percentage. Developers using any major public cloud can expect the target system will run Linux. Evidence that Linux is everywhere has grown in recent years when you add in Android and Linux-based embedded systems in smartphones, TVs, automobiles, and many other devices.
Run Code Online (Sandbox Code Playgroud)

我想缩小它看起来像这样

If you are a software developer in your 20s or 30s, 
you've grown up in a world dominated by Linux. It ha
s been a significant player in the data center for d
ecades, and while it's hard to find definitive opera
ting system market share reports, Linux's share of d
ata center operating systems could be as high as 70%
, with Windows variants carrying nearly all the rema
ining percentage. Developers using any major public 
cloud can expect the target system will run Linux. E
vidence that Linux is everywhere has grown in recent
 years when you add in Android and Linux-based embed
ded systems in smartphones, TVs, automobiles, and ma
ny other devices.
Run Code Online (Sandbox Code Playgroud)

然后放大它看起来像这样:

If you are a software developer in your 20s or 30s, you've grown up in a world dominated by Linux. It has been a significant player in the data center for decades, and while it's hard to find definiti
ve operating system market share reports, Linux's share of data center operating systems could be as high as 70%, with Windows variants carrying nearly all the remaining percentage. Developers using a
ny major public cloud can expect the target system will run Linux. Evidence that Linux is everywhere has grown in recent years when you add in Android and Linux-based embedded systems in smartphones, 
TVs, automobiles, and many other devices.
Run Code Online (Sandbox Code Playgroud)

我该怎么做,我需要将它包含在我的脚本中

小智 6

您可以使用命令fold如下:

fold -w 
Run Code Online (Sandbox Code Playgroud)

w命令 fold 中的标志可以根据包含的列数控制文本(收缩与扩展)。

在你的例子中:

fold -w 52 file 
Run Code Online (Sandbox Code Playgroud)

fold -w 200 file
Run Code Online (Sandbox Code Playgroud)

  • 而且,如果你想像普通文本流那样实际在单词之间断行,你可以使用 `fmt` 而不是 `fold` ... (3认同)