使用这个漂亮的 Unix 工具在 Linux 上处理文本使用这个漂亮的 Unix 工具在 Linux 上处理文本使用这个漂亮的 Unix 工具在 Linux 上处理文本使用这个漂亮的 Unix 工具在 Linux 上处理文本
  • 业务
  • 目标
  • 支持
  • 关于
  • 联系我们
  • 登录
✕

使用这个漂亮的 Unix 工具在 Linux 上处理文本

发表 admin at 2025年2月28日
类别
未分类
标签

pr 工具准备用于打印的文本文档。

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.

(吉姆·霍尔,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 是处理文本的绝佳平台。虽然我们今天使用其他工具,包括网络浏览器中的 HTML 和可打印内容的 PDF,但了解如何使用现有的 Unix 工具创建专业的纯文本文档还是很高兴的。

©2015-2025 Norria Group support@norria.com