2016-07-16 164 views
1

我从日志文件中提取一些错误到一个单独的文件。覆盖输出文件?

我正在寻找一个小块中定义的错误。

#Define all the error types that we need to search on 
$error_6="Missing coded entry in table for provider sector category record" 
$error_7="not a well-formed email address" 
$error_8="Org Id must not contain invalid characters" 
$error_9="Missing sub type code for provider type category record" 
$error_10="Provider sub type" 

然后我在源日志文件中读取并去掉匹配的行。

奇怪的是,如果我将它们转储到单独的文件中,我会在每个文件中获得正确的行数,但是如果使用同一个文件,我只会得到1行。我认为它会附加到该文件。

不工作(只有1行输出)

(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_6 } | Set-Content $path\known_errors.log 
(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_7 } | Set-Content $path\known_errors.log 
(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_8 } | Set-Content $path\known_errors.log 
(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_9 } | Set-Content $path\known_errors.log 
(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_10 } | Set-Content $path\known_errors.log 

作品(总共16行输出)

(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_6 } | Set-Content $path\known_errors_6.log 
(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_7 } | Set-Content $path\known_errors_7.log 
(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_8 } | Set-Content $path\known_errors_8.log 
(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_9 } | Set-Content $path\known_errors_9.log 
(Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_10 } | Set-Content $path\known_errors_10.log 
+0

你一定要明白,通过改变“不匹配”,以“匹配”的答案,你的[前一个问题(http://stackoverflow.com/q/ 38405279/1630171)也为此工作,不是吗? –

回答

1

,或者您可以使用:

 (Get-Content $path\temp_report.log) | Where-Object { $_ -match $error_6 } | out-file $path\known_errors.log -Append