根据字符串删除行

时间:2016-07-15 21:29:20

标签: powershell lines

我有一个错误日志文件,希望从中复制与设置的错误字符串不匹配的所有行。所以基本上我正在构建一个无法识别错误的单独文件。

我定义了所有已知的错误类型

# Define all the error types that we need to search on 
$error_1 = "Start Date must be between 1995 and 9999"
$error_2 = "SYSTEM.CONTACT_TYPE"
$error_3 = "DuplicateExternalSystemIdException"
$error_4 = "From date after To date in address"
$error_5 = "Missing coded entry  for provider type category record"

这就是我在文件中读到的内容

(Get-Content $path\temp_report.log) |
    Where-Object { $_ -notmatch $error_1 } |
    Out-File $path\uncategorised_errors.log
(Get-Content $path\temp_report.log) |
    Where-Object { $_ -notmatch $error_2 } |
    Out-File $path\uncategorised_errors.log
(Get-Content $path\temp_report.log) |
    Where-Object { $_ -notmatch $error_3 } |
    Out-File $path\uncategorised_errors.log

我的输入文件是这样的:

15 Jul 2016 20:02:11,340 ExternalSystemId cannot be the same as an existing provider
15 Jul 2016 20:02:11,340 XXXXXXXXXXXXXXXXXXXXXXXXX
15 Jul 2016 20:02:11,340 ZZZZZZZZZZZZZZZZZZZZZZZZ
15 Jul 2016 20:02:11,340 DuplicateExternalSystemIdException
15 Jul 2016 20:02:11,340 DuplicateExternalSystemIdException
15 Jul 2016 20:02:11,340 SYSTEM.CONTACT_TYPE

当我只有3行时,我的出局完全相同:

15 Jul 2016 20:02:11,340 ExternalSystemId cannot be the same as an existing provider
15 Jul 2016 20:02:11,340 XXXXXXXXXXXXXXXXXXXXXXXXX
15 Jul 2016 20:02:11,340 ZZZZZZZZZZZZZZZZZZZZZZZZ

2 个答案:

答案 0 :(得分:1)

试一试:

(Get-Content $path\temp_report.log) | Where-Object { $_ -notmatch $error_1 -and $_ -notmatch $error_2 -and $_ -notmatch $error_3 -and $_ -notmatch $error_4 -and $_ -notmatch $error_5} | Out-File $path\uncategorised_errors.log

答案 1 :(得分:1)

将已知错误设为数组,使用height

table