I'm trying to dig through some logs and need information before and after the line I can match on. How would I do this in PowerShell ala "grep -C 2"?
我正在尝试挖掘一些日志,并在我可以匹配的行之前和之后需要信息。我如何在PowerShell ala“grep -C 2”中执行此操作?
In version 1, I can't wait for r2, then I get to put it on production machines :)
在版本1中,我不能等待r2,然后我将它放在生产机器上:)
5 个解决方案
#1
The PowerShell equivalent of grep is select-string. You can use the following.
与grep等效的PowerShell是select-string。您可以使用以下内容。
cat file | select-string "pattern" -context 2
Note: this works in PowerShell v2.0 only.
注意:这仅适用于PowerShell v2.0。
#2
Instead of using (gc $bigfile) again, which will cause PowerShell to read in $bigfile to memory on every object piped to it by the ForEach-Object cmdlet, you should probably read the file into a variable and then array index from that, like so:
而不是再次使用(gc $ bigfile),这将导致PowerShell在$ bigfile中读取ForEach-Object cmdlet传送给它的每个对象的内存,您应该将该文件读入变量,然后从中读取数组索引,像这样:
$bigfile = gc 'c:\scripts\bigfile.txt'
$bigfile | Select-String "melissao" | % {$bigfile[($_.LineNumber -3)..($_.LineNumber +1)]}
Also, since the line numbering starts at 1 and array indexing starts at 0 you'll have to go backwards by 3, not 2, to get the line two spaces above "melissao", and go forwards by 1, not 2, to get the line two spaces below "melissao." Doing this will net you the 5 lines you want, "melissao" flanked by the two lines above and below it.
此外,由于行编号从1开始,数组索引从0开始,你必须向后退3而不是2,以获得“melissao”上方两行的行,然后前进1而不是2,以获得“melissao”下面的两行空格。这样做可以为你提供你想要的5行,“melissao”两侧是上面和下面的两条线。
I'm not super familiar with grep -C 2, so I don't know if this replicates that functionality exactly.
我不是非常熟悉grep -C 2,所以我不知道这是否完全复制了这个功能。
#3
Alas, it looks like you will have to build it yourself or wait for the next version of powershell, as it seems to have been implemented there. You can download a pre-release version of Powershell 2.0 from here.
唉,看起来你必须自己构建它或者等待下一版本的PowerShell,因为它似乎已经在那里实现了。您可以从此处下载Powershell 2.0的预发布版本。
#4
Getting closer here- because Select-String returns MatchInfo objects which I can pull a line number out of (39017), now I just need to pull the line surrounding... so:
越来越近 - 因为Select-String返回MatchInfo对象,我可以从(39017)中拉出一个行号,现在我只需要拉出周围的线......所以:
gc $bigfile | Select-String melissao |
%{(gc $bigfile)[($_.LineNumber -2)..($_.LineNumber +2)]}
If anyone could clean this up a bit to make it less slow, you may have the answer. Otherwise, this solution works but obviously not quickly.
如果有人能够将它清理一下以减慢速度,那么你可能会得到答案。否则,此解决方案有效,但显然不会很快。
#5
Download grep for Windows, and call grep from PowerShell?
下载grep for Windows,并从PowerShell调用grep?
#1
The PowerShell equivalent of grep is select-string. You can use the following.
与grep等效的PowerShell是select-string。您可以使用以下内容。
cat file | select-string "pattern" -context 2
Note: this works in PowerShell v2.0 only.
注意:这仅适用于PowerShell v2.0。
#2
Instead of using (gc $bigfile) again, which will cause PowerShell to read in $bigfile to memory on every object piped to it by the ForEach-Object cmdlet, you should probably read the file into a variable and then array index from that, like so:
而不是再次使用(gc $ bigfile),这将导致PowerShell在$ bigfile中读取ForEach-Object cmdlet传送给它的每个对象的内存,您应该将该文件读入变量,然后从中读取数组索引,像这样:
$bigfile = gc 'c:\scripts\bigfile.txt'
$bigfile | Select-String "melissao" | % {$bigfile[($_.LineNumber -3)..($_.LineNumber +1)]}
Also, since the line numbering starts at 1 and array indexing starts at 0 you'll have to go backwards by 3, not 2, to get the line two spaces above "melissao", and go forwards by 1, not 2, to get the line two spaces below "melissao." Doing this will net you the 5 lines you want, "melissao" flanked by the two lines above and below it.
此外,由于行编号从1开始,数组索引从0开始,你必须向后退3而不是2,以获得“melissao”上方两行的行,然后前进1而不是2,以获得“melissao”下面的两行空格。这样做可以为你提供你想要的5行,“melissao”两侧是上面和下面的两条线。
I'm not super familiar with grep -C 2, so I don't know if this replicates that functionality exactly.
我不是非常熟悉grep -C 2,所以我不知道这是否完全复制了这个功能。
#3
Alas, it looks like you will have to build it yourself or wait for the next version of powershell, as it seems to have been implemented there. You can download a pre-release version of Powershell 2.0 from here.
唉,看起来你必须自己构建它或者等待下一版本的PowerShell,因为它似乎已经在那里实现了。您可以从此处下载Powershell 2.0的预发布版本。
#4
Getting closer here- because Select-String returns MatchInfo objects which I can pull a line number out of (39017), now I just need to pull the line surrounding... so:
越来越近 - 因为Select-String返回MatchInfo对象,我可以从(39017)中拉出一个行号,现在我只需要拉出周围的线......所以:
gc $bigfile | Select-String melissao |
%{(gc $bigfile)[($_.LineNumber -2)..($_.LineNumber +2)]}
If anyone could clean this up a bit to make it less slow, you may have the answer. Otherwise, this solution works but obviously not quickly.
如果有人能够将它清理一下以减慢速度,那么你可能会得到答案。否则,此解决方案有效,但显然不会很快。
#5
Download grep for Windows, and call grep from PowerShell?
下载grep for Windows,并从PowerShell调用grep?