根据字符串删除行 [英] Remove lines based upon string

查看:105
本文介绍了根据字符串删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误日志文件,该文件要复制与设置的错误字符串不匹配的所有行.因此,基本上,我正在建立一个无法识别的错误的单独文件.

I have an error log file from which want to copy all lines that DON'T match a set error strings. So basically I am building up a separate file of unrecognised errors.

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

I define all the know error types

# 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

我的输入文件是这样的:

My input file is like this:

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行时,我的出场完全相同:

and my out is exactly the same when I should have only 3 lines:

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

推荐答案

使已知错误成为数组,使用Select-String

Make the known errors an array, use Select-String

$errors=@(
    "Start Date must be between 1995 and 9999",
    "SYSTEM.CONTACT_TYPE",
    "DuplicateExternalSystemIdException",
    "From date after To date in address",
    "Missing coded entry  for provider type category record"
)

Select-String -Path D:\log.txt -NotMatch -Pattern $errors

这篇关于根据字符串删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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