Хвост шаблон с контекстом в Powershell

604
gordy

Если я хватаюсь за бревно вот так:

gc xxx.log -last 1 -wait | sls -patt 'Exception' -simple | % 

он работает, как и ожидалось, выплевывая строки, содержащие «Exception», но когда я пытаюсь добавить некоторый контекст, подобный этому:

gc xxx.log -last 1 -wait | sls -patt 'Exception' -simple -cont 1,2 | % 

..это работает так же, он не содержит никаких строк контекста. Может кто-нибудь сказать мне, почему и что я мог сделать, чтобы получить такие строки контекста?

1

1 ответ на вопрос

1
beatcracker

...it just works the same, it doesn't include any context lines.

Because there is no context! You've explicitly told Get-Content to grab 1 last line with -Last 1, so Select-String receives only one line.

Can anyone tell me why and what I could do to get context lines like this?

Sure, all you have to do, is increase number of lines for the Last parameter. Assuming that you match 1 line in regex and want 1 line before and 2 after it (-Context 1,2) then total amount of lines would be 1 + 1 + 2 = 4:

Get-Content -Path 'xxx.log' -Tail 4 | Select-String -Pattern 'Exception' -SimpleMatch -Context 1,2 | ForEach-Object { $_.Context.PreContext $_.Line $_.Context.PostContext }