我有一个批处理脚本,它将整行搜索字符串打印到一个文本文件中。
for %%i in (log.txt) do (
FINDSTR /G:pattern.txt %%i >> output.txt
)
示例:pattern.txt 包含搜索字符串ERROR
,下面是 log.txt 中的示例文本
2013-06-30 02:17:55,562 INFO Service started
2013-06-30 02:17:55,578 INFO Sending mail...
2013-06-30 02:17:55,578 DEBUG Element value: 1
2013-06-30 02:17:55,578 ERROR error occurred and message is ""
2013-06-30 02:17:55,578 DEBUG bit version: 8
2013-06-30 02:17:55,578 INFO Service stopped
上面的批处理脚本将在 log.txt 中找到字符串时打印每一行文本ERROR
所以,output.txt 看起来像下面的行
2013-06-30 02:17:55,578 ERROR error occurred and message is ""
如何仅打印搜索字符串的上一行和下一行,如下所示:
2013-06-30 02:17:55,578 DEBUG Element value: 1
2013-06-30 02:17:55,578 DEBUG bit version: 8
提前致谢。