2016-07-16 100 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