Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 历史|格雷普<;关键词>;:如何可视化创建的命令周围的更多行?_Linux_Bash_Grep - Fatal编程技术网

Linux 历史|格雷普<;关键词>;:如何可视化创建的命令周围的更多行?

Linux 历史|格雷普<;关键词>;:如何可视化创建的命令周围的更多行?,linux,bash,grep,Linux,Bash,Grep,我使用命令history | grep查找历史记录中具有该特定关键字的所有命令。这很好,但有时还不够:我希望在每个结果之前看到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

我使用命令
history | grep
查找历史记录中具有该特定关键字的所有命令。这很好,但有时还不够:我希望在每个结果之前看到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 
1209git状态
1210吉特加起来。
1211 git提交-m'bla bla'
1212吉特推力http://xxx.xxx.xx.x:xxxxx/
1213吉特标签
1214源工具/设置.sh
1215 cd演示/xxxxxxx/
1216 xxxxxxx
1217 cd演示/等待\zcu102/
1218升

1219 cd build.sw/本机支持您的要求:

  • grep-b1
    在每次匹配前显示一行[B]
  • grep-A 1
    在每次匹配后显示一行[A]
这两个选项可以同时使用,显然,您可以指定任何大于等于0的数字

要完成您的示例:
history | grep-b10-a5构建


有关更多信息,请参阅grep手册的部分。

来自
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

那么,我如何修改原始命令来包含它呢?@Leos313我更新了answerhistory | grep-A5-B10用法:grep[OPTION]。。。模式[文件]。。。有关更多信息,请尝试“grep--help”。没有问题,我已经解决了,对不起。当然,您需要包括
构建