Answered by:
Select a string and check errors

Question
-
Hi guys !
I've a little problem: i have a txt log file, i want search inside it 2 words:
1- Errori: (here will a number, -ge 0 or -eq 0)
2- ERR
If Errori: will be greater than 0 or ERR string is present in the text file,
$ErrorCounts must set to 1, otherwise 0.
Text file:
Text10-08-2017 18:54 ** Backup ultimato per il Task "BACKUP-GIORNALIERO". Errori: 136. File elaborati: 23111. File di backup: 7217. Dimensione totale: 3,17 GB ** 10-08-2017 18:54 *** Backup ultimato. Errori: 0. File elaborati: 23111. File di backup: 7217. Dimensione totale: 3,17 GB *** ERR 10-08-2017 11:08 L'operazione è stata completata con errori
My codePowershell$errorCounts = $null $errorCounts = Get-Content c:\b.txt | Select-String "Errori: (\d+)\." -AllMatches | #Select-String "ERR " -AllMatches | Select-Object -ExpandProperty Matches | Foreach-Object { $_.Groups[1].Value } $errorCounts
Friday, August 11, 2017 8:43 AM
Answers
-
Try this, it's one of the simplest most concise ways of getting it done:
$errorCounts = [int] [bool] (Get-Content C:\b.txt | Select-String "Errori: ([0-9]{2,}|[1-9])\.|^ERR")
This does what you asked for, but doesn't actually *count* how many errors there are in the file, it just tells you *if* there are any errors.
If you really want to find out how many lines contain errors then do this:
$errorCounts = (Get-Content C:\b.txt | Select-String "Errori: ([0-9]{2,}|[1-9])\.|^ERR").Count
- Edited by FaustoNascimento Friday, August 11, 2017 10:19 AM simplified regex
- Marked as answer by Ramses147 Friday, August 11, 2017 10:23 AM
Friday, August 11, 2017 10:11 AM
All replies
-
Hi Ramses,
you can do it like this:
if (gc Test.Txt |grep "Errori: [1-9]|^ERR") { $errorCounts = 1 }
With that filter you'll get all lines that match either of the two.
Cheers,
FredThere's no place like 127.0.0.1
Friday, August 11, 2017 9:40 AM -
Hi Ramses,
you can do it like this:
if (gc Test.Txt |grep "Errori: [1-9]|^ERR") { $errorCounts = 1 }
With that filter you'll get all lines that match either of the two.
Cheers,
Fred
There's no place like 127.0.0.1
- Edited by FaustoNascimento Friday, August 11, 2017 9:59 AM
Friday, August 11, 2017 9:58 AM -
Hi Fausto,
why would that fail? I don't try to capture the number code - checking the highest digit is totally enough to check, whether it is 0 or not (see requirements).
Cheers,
FredThere's no place like 127.0.0.1
Friday, August 11, 2017 10:04 AM -
Try this, it's one of the simplest most concise ways of getting it done:
$errorCounts = [int] [bool] (Get-Content C:\b.txt | Select-String "Errori: ([0-9]{2,}|[1-9])\.|^ERR")
This does what you asked for, but doesn't actually *count* how many errors there are in the file, it just tells you *if* there are any errors.
If you really want to find out how many lines contain errors then do this:
$errorCounts = (Get-Content C:\b.txt | Select-String "Errori: ([0-9]{2,}|[1-9])\.|^ERR").Count
- Edited by FaustoNascimento Friday, August 11, 2017 10:19 AM simplified regex
- Marked as answer by Ramses147 Friday, August 11, 2017 10:23 AM
Friday, August 11, 2017 10:11 AM -
Hi Fausto,
why would that fail? I don't try to capture the number code - checking the highest digit is totally enough to check, whether it is 0 or not (see requirements).
Cheers,
Fred
There's no place like 127.0.0.1
Friday, August 11, 2017 10:14 AM -
Hi FWN,
if you use this ( grep is a bash linux cmd ;-) ):
if (gc c:\script\b.txt | Select-String "Errori: [0-9]|^ERR") { $errorCounts = 1 } $errorCounts
and try with this txt:
10-08-2017 18:54 ** Backup ultimato per il Task "BACKUP-GIORNALIERO". Errori: 136. File elaborati: 23111. File di backup: 7217. Dimensione totale: 3,17 GB ** 10-08-2017 18:54 *** Backup ultimato. Errori: 0. File elaborati: 23111. File di backup: 7217. Dimensione totale: 3,17 GB ***
Result will be $ErrorCounts = 1
While the result should be 0!
If errors in the text file are 0, like in this txt example, $ErrorCounts must be 0It means there are no errors in the log
Friday, August 11, 2017 10:14 AM -
Hi FWN,
if you use this ( grep is a bash linux cmd ;-) ):
if (gc c:\script\b.txt | Select-String "Errori: [0-9]|^ERR") { $errorCounts = 1 } $errorCounts
and try with this txt:
10-08-2017 18:54 ** Backup ultimato per il Task "BACKUP-GIORNALIERO". Errori: 136. File elaborati: 23111. File di backup: 7217. Dimensione totale: 3,17 GB ** 10-08-2017 18:54 *** Backup ultimato. Errori: 0. File elaborati: 23111. File di backup: 7217. Dimensione totale: 3,17 GB ***
Result will be $ErrorCounts = 1
While the result should be 0!
If errors in the text file are 0, like in this txt example, $ErrorCounts must be 0It means there are no errors in the log
That is why FWN made it [1-9] and not [0-9], but he is still assuming that errors will never have leading zero(s).
My proposed approach does not suffer from that problem.
Friday, August 11, 2017 10:18 AM -
Are you italian ?
Great Fausto, working !
Thanks FWN too, have a good day guys !
Friday, August 11, 2017 10:24 AM -
Hi FWN,
if you use this ( grep is a bash linux cmd ;-) ):
if (gc c:\script\b.txt | Select-String "Errori: [0-9]|^ERR") { $errorCounts = 1 } $errorCounts
and try with this txt:
10-08-2017 18:54 ** Backup ultimato per il Task "BACKUP-GIORNALIERO". Errori: 136. File elaborati: 23111. File di backup: 7217. Dimensione totale: 3,17 GB ** 10-08-2017 18:54 *** Backup ultimato. Errori: 0. File elaborati: 23111. File di backup: 7217. Dimensione totale: 3,17 GB ***
Result will be $ErrorCounts = 1
While the result should be 0!
If errors in the text file are 0, like in this txt example, $ErrorCounts must be 0It means there are no errors in the log
Hi Ramses,
just to be sure: The requirement was for either a line starting with ERR or with any line having Errori: 1+. I checked for that and it reports true (the first line in this example has a value of 136).
Was only the second line to be interpreted? Will it always be the second line?
Cheers,
FredThere's no place like 127.0.0.1
Friday, August 11, 2017 10:31 AM -
The two things to check, to have $ ErrorCounts = 0 in the log are as follows:
1- That there is no ERR written
2- Must be "Errors: 0"
So if the characters "ERR" are present and if the errors are greater or equal to 1, $ ErrorCounts will be equal to 1Friday, August 11, 2017 12:22 PM