如何从 bash 的历史记录中选择行的子集?
我经常通过 bash shell 历史记录来 grep
来查找旧命令、文件路径等。确定了感兴趣的历史记录编号后,我希望在两侧看到几行上下文,即查看历史线的子集。例如:
$ history | grep ifconfig
8408 ifconfig eth0
8572 sudo ifconfig eth0 down
我想查看第 8572 行两侧的 5 行左右。显然知道行号,我可以用 less
翻阅历史记录,但这看起来非常愚蠢。据我所知,联机帮助页似乎也没有此信息。
有没有一种简单的方法可以在 bash 中检索任意历史记录行?
Quite often I grep
through my bash shell history to find old commands, filepaths, etc. Having identified the history number of interest, I would like to see a few lines of context on either side, i.e. view a subset of history lines. For example:
$ history | grep ifconfig
8408 ifconfig eth0
8572 sudo ifconfig eth0 down
I would like to look at the 5 lines or so either side of line 8572. Obviously knowing the line number I can page through the history with less
, but this seems very stupid. As far as I can tell, the manpage doesn't seem to have this information either.
Is there a simple way to retrieve arbitrary lines of history in bash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
grep 的 -C 选项提供上下文。尝试:
grep's -C option provides context. Try:
如果您只想查看特定行号,您也可以使用类似的示例,
该示例打印第 116 行到第 120 行。
If you only want to see it for specific line numbers, you can also use something like this
The example prints lines 116 through 120.
A = grep 找到的匹配项后的行数
B = grep 找到的匹配项之前的行数
您还可以将行数从 5 更改为您想要的任何数量。
A = number of lines after the match found by grep
B = number of lines before the match found by grep
You can also change the number of lines from 5 to any number you want.
输入 ctrl-r,然后输入一些字符(交互式搜索)。更多信息。
type ctrl-r, then some characters (interactive searching). More information here.
我输入
历史 | grep“840”
I type
history | grep " 840"