使用这款出色的 Unix 工具在 Linux 上处理文本

pr 工具用于准备用于打印的文本文档。
4 位读者喜欢这篇文章。
5 trends in open source documentation

Internet Archive Book Images。由 Opensource.com 修改。CC BY-SA 4.0

Unix 在文本处理方面一直很出色,Linux 也不例外。用于处理和转换文本文件的工具仍然存在于所有 Linux 系统中。

像其他计算机系统一样,早期的 Unix 使用打字机式打印设备在纸上打印。这些打印机提供的格式化选项有限,但通过巧妙地应用 Unix 工具,您可以准备看起来专业的文档。

其中一个工具是 pr 工具,用于准备用于打印的文本文档。让我们探索如何使用标准的 Unix 工具,例如 pr 处理器和 fmt 文本格式化器,来准备在打字机式打印机上打印的文本文件。

[ 另请阅读: 我如何使用 Linux fmt 命令来格式化文本 ]

打印纯文本文件

假设我们想要打印 MIT 许可证,它存储在一个名为 mit.txt 的文件中。此文件已经过格式化,以实现最佳屏幕显示;行宽几乎为 80 列,这非常适合标准终端。

$ cat mit.txt 
Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Image of standard 80 column terminal

(Jim Hall, CC BY-SA 40)

打印机纸张的宽度也是 80 列,至少在经典打印机上是这样。因此,我们也可以使用类似 lpr 的命令将文件打印到我们的打印机。但这并不是一种非常有趣的打印文件的方式,而且阅读起来也不会很舒服。例如,文档将从打印页面的第一行开始,并立即位于纸张的左边缘。

我们可以使用 pr 命令添加顶部边距,使文档更易于阅读。默认情况下,pr 在顶部页眉中包含日期和时间、文件名和页码。例如,我们文件的顶部可能看起来像这样

$ pr mit.txt | head


2022-06-24 18:27                     mit.txt                      Page 1


Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights

在这个例子中,我使用了 head 命令来查看 pr 输出的前十行。pr 命令还在页面底部添加额外的空白行,以提供底部边距。旧式的打字机打印机每页使用 66 行,因此 pr 输出也假定如此。但是这个文件打印在一页上,所以我不需要显示文件的底部;它只是一些空白行。

添加左边距和右边距

添加顶部边距使文档更易于阅读,但我们可以通过在打印页面的左侧和右侧添加空间来做得更好。这有效地为我们的文档添加了左边距和右边距。

第一步是使用 fmt 命令将文本文件重新格式化为不同的宽度。使用 fmt -w 70 将文本文件重新格式化为使用 70 列宽的行。我们可以在文档的左侧添加一些空白空间来创建左边距。使用 pr -o 5 在输出的每一行的开头添加 5 个空格。使用较窄的文本,我们还将在右边距中留出大约 5 个空格。

$ fmt -w 70 mit.txt | pr -o 5 | head
     

     2022-06-24 18:35                                                  Page 1


     Copyright (c) <year> <copyright holders>
     
     Permission is hereby granted, free of charge, to any person obtaining
     a copy of this software and associated documentation files (the
     "Software"), to deal in the Software without restriction, including

这就是 Unix 用户打印纯文本文件的方式。您可以使用相同的命令集在现代激光打印机上打印文本文件,但您的打印机可能期望页面进纸命令而不是使用空白行。为此,请将 -f 选项添加到 pr 命令,如下所示

$ fmt -w 70 mit.txt | pr -f -o 5 | lpr

我将在本文的其余部分省略 -f,但如果您想将文档打印到现代激光打印机,请记住将 -f 添加到 pr 命令。

更改页眉

您可能会注意到,当我们把 fmt 的输出重定向到 pr 命令时,pr 输出不再显示文件名。那是因为当我们像这样将几个命令链接在一起时,pr 命令不知道文件名,所以它留空了。我们可以使用 -h 选项将文件名添加到页眉

$ fmt -w 70 mit.txt | pr -h 'mit.txt' -o 5 | head
     

     2022-06-24 18:45                     mit.txt                      Page 1


     Copyright (c) <year> <copyright holders>
     
     Permission is hereby granted, free of charge, to any person obtaining
     a copy of this software and associated documentation files (the
     "Software"), to deal in the Software without restriction, including

您可以对页眉进行其他更改,例如使用 -D 选项更改日期和时间格式,或将其替换为新文本。

$ fmt -w 70 mit.txt | pr -D '6/24/2022' -h 'mit.txt' -o 5 | head -30
     

     6/24/2022                         mit.txt                         Page 1


     Copyright (c) <year> <copyright holders>
     
     Permission is hereby granted, free of charge, to any person obtaining
     a copy of this software and associated documentation files (the
     "Software"), to deal in the Software without restriction, including
     without limitation the rights to use, copy, modify, merge, publish,
     distribute, sublicense, and/or sell copies of the Software, and to
     permit persons to whom the Software is furnished to do so, subject
     to the following conditions:
     
     The above copyright notice and this permission notice shall be
     included in all copies or substantial portions of the Software.
     
     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
     KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
     BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
     IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     THE SOFTWARE.

打印两列

如果您想让文本文档在打印页面上看起来非常精美怎么办?某些文档(例如技术文章)可能需要以两列布局打印。pr 命令可以以多列打印文本。例如,-2 以两列打印,-3 将以三列打印。

但是,在以多列打印文本时要小心。如果行太长,pr 可能会简单地将一列与另一列重叠,从而有效地丢失输出中的文本。但是我们可以利用 fmt 命令将文本重新格式化为更窄的宽度,以适合以两列格式打印。

让我们做个数学计算:如果打印页面的宽度为 80 列,并且我们在左侧和右侧留出了 5 个空格作为页面边距,那么我们的文本还剩下 70 列。使用 fmt -w 35 会将文本整齐地分成两半以用于两列,但我们可能不会在列之间留下太多空间。相反,让我们使用 fmt -w 33 将文本宽度重新格式化为 33,然后再将输出发送到 pr 命令

$ fmt -w 33 mit.txt | pr -2 -D '6/24/2022' -h 'mit.txt' -o 5 | head -30
     

     6/24/2022                        mit.txt                         Page 1


     Copyright (c) <year> <copyright     be included in all copies or
     holders>                           substantial portions of the
                                         Software.
     Permission is hereby granted,       
     free of charge, to any person       THE SOFTWARE IS PROVIDED
     obtaining a copy of this            "AS IS", WITHOUT WARRANTY OF
     software and associated             ANY KIND, EXPRESS OR IMPLIED,
     documentation files (the            INCLUDING BUT NOT LIMITED TO THE
     "Software"), to deal in the         WARRANTIES OF MERCHANTABILITY,
     Software without restriction,       FITNESS FOR A PARTICULAR PURPOSE
     including without limitation the    AND NONINFRINGEMENT. IN NO
     rights to use, copy, modify,        EVENT SHALL THE AUTHORS OR
     merge, publish, distribute,         COPYRIGHT HOLDERS BE LIABLE
     sublicense, and/or sell copies      FOR ANY CLAIM, DAMAGES OR OTHER
     of the Software, and to permit      LIABILITY, WHETHER IN AN ACTION
     persons to whom the Software is     OF CONTRACT, TORT OR OTHERWISE,
     furnished to do so, subject to      ARISING FROM, OUT OF OR IN
     the following conditions:           CONNECTION WITH THE SOFTWARE
                                         OR THE USE OR OTHER DEALINGS IN
     The above copyright notice and      THE SOFTWARE.
     this permission notice shall




$ 

Unix 是一个用于处理文本的绝佳平台。虽然我们今天使用其他工具,包括 Web 浏览器中的 HTML 和用于可打印内容的 PDF,但很高兴知道如何使用现有的 Unix 工具来创建专业的纯文本文档。

标签
photo of Jim Hall
Jim Hall 是一位开源软件倡导者和开发者,以 GNOME 中的可用性测试以及作为 FreeDOS 的创始人兼项目协调员而闻名。

评论已关闭。

Creative Commons License本作品根据 Creative Commons Attribution-Share Alike 4.0 International License 获得许可。
© . All rights reserved.