regex - 使用终端 man 命令阅读手册页中的部分

标签 regex linux bash awk gnome-terminal

我只想阅读手册页中我要查找的部分。

例如只阅读 man 手册页中的 -k 部分我可以使用

man man | grep -E '(-k)'

结果:

man -k [apropos options] regexp ...
man -k printf
-k, --apropos
       which in turn overrides the $PAGER environment variable.  It is not used in conjunction with -f or -k.

结果不好,因为不是我需要的所有部分。
所以目前我正在使用:

man man | grep -E '(-k|'')'

结果是所有文本,但用红色标记了 -k

我的问题是我怎样才能得到类似这样的输出:

man -k [apropos options] regexp ...
man -k printf
       Search the short descriptions and manual page names for the keyword printf as regular expression.   Print  out  any  matches.
       Equivalent to apropos printf.
-k, --apropos
          Equivalent to apropos.  Search the short manual page descriptions for keywords and display any  matches.   See  apropos(1)
          for details.

最佳答案

你应该考虑 -k (-AnyOther) 的部分是什么意思

例如这个条目呢?:

   -P pager, --pager=pager
          Specify which output pager to use.  By default, man uses pager -s.  This option overrides the $MANPAGER environment variable, which in turn overrides the $PAGER environment  variable.   It
          is not used in conjunction with -f or -k.

它也以某种方式与 -k 相关。

根据您的示例,我想您想跳过此条目。 如果是这样,我建议使用两个正则表达式:

"^\s*-k" # for the case when -k is at the beginning
"man -k" # self explainable :) 

从找到的条目中,您应该打印下面的所有行,在第一个可打印字符之前的空格比您的匹配中多。

我不确定 grep 或 sed 是否适合这项工作。就我个人而言,我会尝试使用 python 或 awk 来完成。

关于regex - 使用终端 man 命令阅读手册页中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40466255/

相关文章:

regex - T SQL 模式或正则表达式,以确保为 varchar 列返回的所有行都严格是字母数字

bash - 将 ISO8601 时间转换为 unix 时间戳的命令行

git - 将 parse_git_branch 函数从 bash 翻译成 zsh(用于提示)

python - 从 Windows 迁移到 Linux

linux - Bash 确定最高变量和最低变量之间的差异

c++ - 创建自定义 shell

regex - 正则表达式中的方括号问题

Python正则表达式分割但将正则表达式匹配的结束部分放回到字符串中?

python - 将变量指定为以正则表达式运算符 '^' 开头的值

在 linux 下使用 C 控制 C920 Logitech HD Pro 网络摄像头