历史 |grep <key_word>:如何在建立的命令周围可视化更多行?



i使用命令history | grep <key_word>使用该特定 key_word 来查找历史记录中的所有命令。太好了,但有时还不够:我很想看到n个命令,每个结果后的m命令。

例如,使用history | grep build,我想在每个结果之后查看n = 10个命令,并在每个结果之后使用m = 5命令。针对以下理想输出:

 1209  git status
 1210  git add .
 1211  git commit -m 'bla bla bla'
 1212  git push http://xxx.xxx.xx.x:xxxxx/
 1213  git tag 
 1214  source tools/settings.sh 
 1215  cd demos/xxxxxxx/
 1216  xxxxxxx 
 1217  cd demos/wait_zcu102/
 1218  ls
 1219  cd build.sw/      <------------------------------------- <key_word> is here
 1220  ls
 1221  atom .
 1222  source tools/settings.sh 
 1223  cd demos/xxxxxxxxx/
 1224  xxxxx 

有没有办法做到这一点?

您要问的是本机支持:

  • grep -B 1 <key_word>在每个匹配之前显示一行[b] e
  • grep -A 1 <key_word>显示一行[a] fter fter fter

这两个选项都可以同时使用,显然您可以指定任何数字> = 0。

完成您的示例:history | grep -B 10 -A 5 build

有关更多信息,请查看Grep Man的上下文控制部分。

来自 grep的文档:

 -A num, --after-context=num
         Print num lines of trailing context after each match.
 -B num, --before-context=num
         Print num lines of leading context before each match.
 -C[num, --context=num]
         Print num lines of leading and trailing context surrounding each match. The default is 2 and is equivalent to -A 2 -B 2.

在上面的示例中,您可以使用:

history | grep -A5 -B10 build

最新更新