Windows 等价于 grep -B(上下文前)和 -A(上下文后)? [英] Windows equivalent to grep -B (before context) and -A (after context)?

查看:48
本文介绍了Windows 等价于 grep -B(上下文前)和 -A(上下文后)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

grep 中,我知道有一个 -B 开关可以...

In grep I understand there is a -B switch that will...

在匹配行之前打印 NUM 行前导上下文.

Print NUM lines of leading context before matching lines.

...因此,例如您可能会运行如下命令:

... so for example you might run a command like:

cvs log -N -S -w<userid> -d"1 day ago" | grep -B14 "some text" > afile

在 Windows 中是否有与此等效的东西?我目前得到的是:

Is there an equivalent to this in Windows? What I've currently got is:

cvs log -N -S -w<userid> -d"1 day ago" | find "7492" > output.txt

但这只是在输出中与 7492 同一行的文本,而我需要前面的一些行来有用地解释信息.但就我所见,find 命令没有与 grep-B 开关等效的开关.有没有办法在 Windows 中复制 grep 功能的这一方面?

But this only pipes the text on the same line as 7492 in the output, whereas I need some of the preceding lines to usefully interpret the information. But as far as I can see the find command doesn't have a switch equivalent to the -B switch of grep. Is there some way of replicating this aspect of grep functionality in Windows?

推荐答案

使用 select-string-context 参数.示例:

Use select-string's -context argument. Example:

git log | select-string limit -context 5,0

这是前 5 行,后 0 行.这个例子碰巧得到了与 git commit 消息匹配的哈希值.

This is for 5 line before, 0 lines after. This example happens to get the hash that matches a git commit message.

如果您输入一个数字作为此参数的值,则该数字决定了匹配前后捕获的行数.如果输入两个数字作为值,第一个数字确定匹配前的行数,第二个数字确定匹配后的行数.

If you enter one number as the value of this parameter, that number determines the number of lines captured before and after the match. If you enter two numbers as the value, the first number determines the number of lines before the match and the second number determines the number of lines after the match.

请参阅 MSDN 文档,了解 <代码>选择字符串

这篇关于Windows 等价于 grep -B(上下文前)和 -A(上下文后)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆