我发现在命令行的手册页中,grep对于文本(例如特定选项)很有用。
但是,当在手册页上操作时,grep并不像预期的那样工作,如下所示,模式"-f"只匹配"-",而模式"--file"不匹配任何东西(alias grep='grep --color=always'):
nlykkei-mbp:~ nlykkei$ alias grep
alias grep='grep --color=always'
nlykkei-mbp:~ nlykkei$ man grep | grep -e "-f"
[-e pattern] [-f file] [--binary-files=value] [--color[=when]]
-F, --fixed-strings
-f file, --file=file
-h, --no-filename
--binary-file=without-match option.
-L, --files-without-match
-l, --files-with-matches
--binary-files=value
and the behaviour of the -f flag when used with an empty pattern file is
nlykkei-mbp:~ nlykkei$ man grep | grep -e "--file"
nlykkei-mbp:~ nlykkei$ echo "--file" | grep -e "--file"
--file
nlykkei-mbp:~ nlykkei$ ▒ 相反,匹配来自echo的管道文本就像预期的那样工作,所以这与手册页的“不可见”格式有关吗?是否有可能在手册页中提供可靠的grep文本?
注意:我知道man -k和man -K,但这些并不能完全解决我想要实现的目标。
发布于 2019-10-26 20:34:25
解决方案1:为man使用特定的conf文件
配置文件.man.raw.conf
NROFF /usr/bin/groff -mandoc -Tlatin1 -P-u -P-b一个使用man的例子
MyMAC:tmp e444$ man -C .man.raw.conf grep | grep -e --file
-f file, --file=file
-L, --files-without-match
-l, --files-with-matches解决方案2:使用colcrt -
MyMAC:tmp e444$ man grep | colcrt - | grep -e --file
-f file, --file=file
-L, --files-without-match
-l, --files-with-matchesps 1:如果没有-,colcrt会添加更多的垃圾
ps 2:这个问题和答案在linux上没有意义,因为不同的行为是不同的。
发布于 2019-10-27 17:54:53
您应该在手册页上使用grep,而不是。手册页被编码(不是用简单的英语),内容将与你想要搜索的句子不匹配。
假设程序man使用less显示手册页,那么有一种在less帮助下进行搜索的方法。继续往下读。
要确认您的手册页是由less显示的,输入man grep (或其他命令),按enter并在查看手册页时,只按h键。如果它较少,则显示帮助页的标题为:SUMMARY of less命令。一旦你确认了。您应该知道,通过使用/键,less可以自己进行搜索(如果需要,按下q以quit显示的帮助页)。如果你按:
/-f然后按enter键,您将看到突出显示的-f (预置n将移动到下一次匹配)。
或者,如果只需要看到与字符串匹配的行,请执行以下操作:
&-f如果需要从命令行激活此类搜索,请执行以下操作:
$ LESS=+'/-f' man grephttps://unix.stackexchange.com/questions/548665
复制相似问题