1

I´d like to use PS to scan some log files for errors

select-string -pattern '[E]' -path D:\15\s\BAS\VectorNetworkAnalysis.AutomationInterfaceTests\Tests\COMTesting\PythonUnitTests\PyTestsResult*.log 

but despite there are NO ERRORS PS lists every line of the logs as a hit, at least the ones containing "[i]". I tried different variations, with double apostrophes, blanks before/after the [E] and intentionally added one single line containing "[E]" but either I´getting a bunch of lines or none.

Any hint? Thanks a lot.

here is an example of log including the line with the INTENTIONALLY ADDED [E]

2022-03-04 09:22:26 [i] Transmission: Execution State is OK!
2022-03-04 09:22:26 [i] User range calib state is active!
2022-03-04 09:22:28 [i] **** Starting Save/Load calibration test ****
2022-03-04 09:22:29 [i] Transmission: Execution State is OK!
2022-03-04 09:22:29 [i] User range calib was saved!
2022-03-04 09:22:29 [i] User range calib was loaded!
2022-03-04 09:22:31 [i] **** Starting S11 one port meas test ****
2022-03-04 09:26:04 [i] S11OnePortMeas: Execution State is OK!
2022-03-04 09:26:04 [i] S11OnePortMeas: Number of results is OK!
2022-03-04 09:26:05 [E] **** Starting Impedance one port meas test ****
2022-03-04 09:26:06 [i] OnePortMeas: Execution State is OK!
AlexxRR
  • 11
  • 2
  • 1
    If I got you right ... the square brackets in your pattern specify a *character class*. So every **e** will match. If you're looking for literal square brackets you will have to escape them. ... like `'\[e\]'` – Olaf Mar 04 '22 at 10:05
  • U R my hero! Thanks so much! – AlexxRR Mar 04 '22 at 10:16
  • Note that `'\[e\]'` will only match lowercase "e" and `'\[E\]'`will only match uppercase "E". If you want to match both you can use `'\[[eE]\]'`. – anto418 Mar 04 '22 at 10:23
  • @anto418 Since PowerShell is by default case insenstitive it doesn't matter. ;-) – Olaf Mar 04 '22 at 10:42
  • Good point, I assumed it would be case-sensitive since regex is. – anto418 Mar 04 '22 at 12:12
  • 1
    For `Select-String` you would need to use the `-CaseSensitive` if you want to specifically match upper case `\[E]`, you don't need to escape both brackets btw. See `[regex]::Escape('[E]')`. – Santiago Squarzon Mar 04 '22 at 13:10

2 Answers2

1

Select-String -Pattern uses regex, and in regex, brackets means "match any of the characters in the brackets", so it matches every line that contains an E. You could replace [E] with [qwerty] and it would match all lines that contains any of the letters in "qwerty" (so about all of them in your example).

If you want to match a string literally, you can use the -SimpleMatch parameter which will interpret your input as a string instead and match anything containing that given string.

anto418
  • 176
  • 1
  • 13
0

Use a website such as regex101.com to test regular expressions before placing them in a script. This has saved me a lot of time and effort.

(?i)\[e\] matches both [e] and [E].

\[e\] matches [e].

\[E\] matches [E].

The (?i) makes the regular expression case insensitive. Regular expressions have their own case sensitivity rules independent of PowerShell.

Darin
  • 1,423
  • 1
  • 10
  • 12
  • Again: PowerShell in general is case insensitive by default. That is valid for its regex implementation. If you want it to act case sensitive you have to tell it explicitly to do so. ;-) – Olaf Mar 04 '22 at 12:55