linux极简小知识:37、将man手册文档转换保存为txt文本格式,col命令介绍

1,845 阅读3分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

本文已参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金。

man保存文本

先直接说明方法:

保存man手册文档为文本格式,常用方法为:man command | col > fileman command | col -b > file

比如:

top 命令的帮助手册 :

man top | col > top_help.txt

mkdir命令 :

man mkdir | col -b > mkdir-help.txt

为什么使用col命令

col用于过滤反向换行、控制字符等内容,使文本更易于阅读和保存。

但实际上,大多数使用man直接重定向保存,通常也不会有乱码。比如 man command > file

作为习惯或标准的处理。通常都是 man command | col -b > file

也就是说,如果仅仅是简化的想要保存一个命令的帮助文档内容,直接使用 man command > file 就方便很多;而如果遇到保存的帮助文档文件有乱码,或无法显示的字符,则应该结合 col。

现代帮助文档,已经很少出现反向换行或阅读不便的空白字符了,处理的基本都是以 Unicode 编码为基础!

col命令

介绍

Linux 系统中的 col 命令用于过滤掉反向换行(reverse line feeds),这样输出看起来更符合正确的顺序 —— 只有正向和半正向换行,并尽可能用制表符(tabs)替换空白字符(whitespace characters)。这在处理 nroff 和 tbl 的输出时被证明很有用。

col 实用程序从标准输入读取并写入标准输出。

下面是对col的英文描述:

col command in Linux system is used to filter out reverse line feeds such that the output looks much more in the correct order with only forward and half forward line feeds and replaces the whitespace characters with tabs wherever possible. This can be proven useful in the processing of the output of nroff and tbl.

The col utility simply reads from the standard input and writes to standard output.

nroff 和 troff 是用于文本格式化的工具,很古老强大的工具。在格式化表格时,两者都使用 tbl 作为预处理器。

RUNOFF 的名称来自短语“to run off a document”,意思是完成文档,或准备发布、演示文档。

The name RUNOFF comes from the phrase "to run off a document," meaning to finalize a document, or prepare it for publication or presentation.

  • nroff —— "new runoff"/"newer runoff"
  • troff —— "typesetter roff"
  • Groff —— 指的是 GNU troff 项目

推荐参考 Linux troff commandLinux nroff command

col命令不存在时的安装

# RHEL
yum install noweb

#Ubuntu
apt-get install noweb

语法参数

col [-bfhpx][-l <缓冲区列数>] 

参数:

  • -b 过滤掉所有的控制字符,包括 RLF 和 HRLF 。
  • -f 滤除 RLF 字符,但允许将 HRLF 字符呈现出来。
  • -h 不允许显示多个空格替代制表符
  • -p 强制未知控制序列不变。通常,除了自己识别和解释的那些控制序列之外,col会从输入中过滤掉任何控制序列。
  • -x 以多个空格字符来替代tabs字符(制表符)。
  • -l <缓冲区列数> 预设的内存缓冲区有128列(默认值),您可以自行指定缓冲区的大小。

参考

主要参考自 col command in Linux with Examples菜鸟教程