Home > Mobile >  powershell select-string -pattern -path finds hits where there are none
powershell select-string -pattern -path finds hits where there are none

Time:03-05

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!

CodePudding user response:

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.

CodePudding user response:

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.

  • Related