PowerShell尾文件,但忽略匹配模式的行

时间:2020-05-20 04:07:39

标签: powershell grep

Solaris管理员,试图在Windows世界中使用日志文件。 我正在尝试拖尾一个日志文件,但是有6条重复的行我想忽略。 我已经可以选择以下行:

var car = ["Abarth", "Acura", "Alfa Romeo", "AMC", "Aston Martin", "Autobianchi", "Audi", "Austin", "Bentley", "BMW"];

console.log(car.splice(2));

但是我试图忽略这些“上下文”行。我尝试在末尾添加Get-Content alert.log -Tail 300 -Wait | Select-String -Pattern "WARNING: too" -Context 1,6 ,但最终打印出我要忽略的行。

在Solaris中,我会这样做:

-NotMatch

我试图找到一个等效的PowerShell,但没有成功。我已经成功地忽略了一条匹配的线:

tail -300f alert.log | egrep -B1 -A6 -v "WARNING: too"

但是如何将其扩展到上方的行和下方的6行? 我要忽略的行类型为:

Get-Content alert.log -Tail 300 | ?{$_ -notmatch 'WARNING: too'}

操作:我已经能够创建以下语句:

2020-05-20T15:32:13.870875+10:00
WARNING: too many parse errors
PARSE ERROR:
2020-05-20T15:32:13.886503+10:00
begin ORACLE PL/SQL; end;
Additional information: hd=00007FF6E4A08E68
...Current username=FRED
...Application: work3wp.exe

但是每次都会出现错误开始处的日期/时间戳。我想要其他错误的日期/时间戳。

1 个答案:

答案 0 :(得分:0)

PowerShell是一种面向对象的语言,而不是文本。字符串只是另一个对象。

根据您的用例,您可以执行以下操作。当然,也许不如您在* nix中显示的那样直接,但是PowerShell当然不是* nix,我们必须使用PowerShell方式进行操作。

这就是我想出的供您考虑的内容。在PowerShell中,总是有六种不同的方法来执行X或Y事务。有些比其他的更优雅。

Get-Content -Path 'D:\temp\abc.txt'

<#
# Results

a=abcdef123
b=ngh567
c=defh123
#>


Get-Content -Path 'D:\temp\abc.txt' -Tail 3 -Wait | 
ForEach{$PSItem -notmatch 'c'}

# After adding test to the file and saving
<#
# Results


False
True
False
True
True
#>

# After adding checking to the file and saving
<#
# Results


False
True
False
True
True
True
False
#>

Get-Content -Path 'D:\temp\abc.txt' -Tail 3 -Wait | 
ForEach{Select-String -InputObject $PSItem -NotMatch 'context'}

<#
# Results


a=abcdef123
b=ngh567
c=defh123
#>

# After adding a string to the file and saving

<#
# Results

testing
#>


# After adding context to the file and saving
# no response


# after adding a new filtered string

<#
# Results

after c*text
#>

# after adding 3 lines including 1 filtered string and saving

<#
# Results

blank line before c*text
line after c*text
#>

# What's in the file now
Get-Content -Path 'D:\temp\abc.txt'

<#
# Results

a=abcdef123
b=ngh567
c=defh123
testing
context
after c*text
blank line before c*text
context
line after c*text
#>