Using the Linux fold command to make text more readable

在Linux中,折叠命令可以包装比监视器的宽度更宽的文本。

  • Share on Facebook
  • 分享到Twitter
  • 分享LinkedIn
  • Share on Reddit
  • Share by Email
  • 打印资源
paper boat made out of twenty british pounds sterling currency bank note by wragg getty images 1821
wrag / getty图像

这Linux折叠根据您提供的论点,将文字行并将其分解为块。没有争论,折叠will break lines at 80 characters.

这first example below uses a single-line text file that includes indications of character positions. First, we count the number of characters and lines in the file using theWC -LandWC -L命令:

美元

因此,此文件具有251个字符(包括运输返回)和一行文本。接下来,我们使用cat命令:

$ cat wide_text .........1.........2.........3.........4.........5.........6.........7.........8.........9........10........11........12........13........14........15........16........17........18........19........20........21........22........23........24........25

这n we use the折叠command to break the file into separate lines:

$ fold wide_text ......... 1 ......... 2 ........................................................................................................................................................................................5 ................................................ 7 ......... 8 ....................................................................................................。..... 16 ........ 17 ...............................................................................................................................................................21 ........ 22 ........................................................................................................... 25

在上面的示例中,将250个字符线传递给折叠命令,并生成四行文本 - 三个具有80个字符的文本,其余10个字符。

折叠但是,命令确实提供了-c(字符)将线路分解成大块或较小的块的选项。在下面的示例中,我们将wide_text文件内容分为50个字符的块。

$ fold -c50 wide_text .........1.........2.........3.........4.........5 .........6.........7.........8.........9........10 ........11........12........13........14........15 ........16........17........18........19........20 ........21........22........23........24........25

Folding the text and creating a new file in one step is easy:

$ fold -c50 wide_text folded_text $ wc -l folded_text;wc -c folded_text 5 folded_text 255 folded_text

如您所见,folded_text文件包含五行文本和255个字符。你也可以折叠lines by bytes instead of characters using the-b当有用时选项。在某些情况下,这会改变输出。

现在,让我们看一个更现实的示例,在该示例中,我们将一条很长的行分解为更合理的线路进行查看。

首先,要查看文件中的每一行中有多少个字符,我们使用如下所示的脚本。他们俩如果顶部的命令检查已提供文件名并存在指定文件。这尽管命令在底部显示文件中每行的长度。

#!/bin/bash # check for an argument if [ $# -eq 0 ]; then echo -n "file: " read file else file=$1 fi # make sure the file exists or exit if [ ! -f $file ]; then echo "ERROR: cannot find $file" exit 1 fi $ count characters in each line while read line; do echo $line | wc -c done < $file

该脚本显示文件中每行的长度,显然比在终端窗口中显示的要多得多,因此它是折叠的好候选者。

$ chars-per-line lorem_ipsum |头-6 7 137 149 153 154 35

如果我们将此文件分解为80个字符行,我们可能会使用以下几个世纪的旧“ Lorem Ipsum”文本看到类似的示例:

$ fold lorem_ipsum lorem ipsum dolor sit amet,insectetur adipiscing elit,sed do eiusmod permure i ncididunt ut ut labore et dolore magna aliqua。UT Enim ad Minim veniam,Quis Nostru D练习Ullamco Laboris nisi ut equip equip exe ea compocoto compocate。Duis Aut e Irure Dolor在velledenderit中发出了Velit velit ese ese cillum dolore eu fugiat n ulla Pariatur。除了sint occaecat cupidatat non Proident,在culpa qui quartia deserunt mollit anim and Manderum中晒太阳。

As you can see from the example above, breaking text files at a fixed position won't likely be a good choice since it would probably break some words into two pieces.

当我们添加-s(break at spaces) option, we get a much more useful rendering of the text:

$ fold -s lorem_ipsum lorem ipsum dolor sit amet,insectetur adipiscing elit,sed do eiusmod eriusmod terme ristidunt ut ut labore et dolore magna aliqua。UT Enim ad Minim veniam,Quis Nostrud练习Ullamco Laboris nisi ut equip equip ex ea Commodo Reactation。duis aute irure dolor in prechenderit in Voluptate velit esse sepe cillum dolore eu fugiat nulla Pariatur。除了sint occaecat cupidatat non Proident,在culpa qui quartia deserunt mollit anim and Manderum中晒太阳。

Note: Text files will be a little larger when folded with the折叠命令是因为它们将包含更多的newline(马车返回)字符。

包起来

折叠当您想将文本段落变成没有那么多字符并打破单词边界的单独行时,命令可能非常有用。这可以使观看大型文本变得更加容易。

加入网络世界社区足球竞猜app软件FacebookandLinkedInto comment on topics that are top of mind.
有关的:

版权©2022 I足球竞彩网下载DG Communications,Inc。