Author Posts

July 7, 2014 at 4:33 am

Hi,

I have got some trouble with the switch statement because I have to check log statements from a logfile.

The statements could be for example:

cvs [update aborted]: no such user XYZ
cvs update: cannot open XYZ
cvs update: No CVSROOT specified!

but also...

? XYZ
U XYZ
M XYZ
R XYZ

In the real logfile XYZ is changed by a file- or directory name.

I'm looking for a simple switch statement for checking the logfile string. My problem is the wildcard symbol "?" that is included in one logfile statement. When I use the -wildcard option for the last 4 examples are the same for powershell.

Do you have any ideas for me?

Thanks

July 7, 2014 at 5:29 am

If you need to match a literal ?, * or square bracket character in a wildcard string, place that character inside a set of square brackets:

$strings = 'Whatever UXYZ Whatever', 'Whatever ?XYZ Whatever'

switch -Wildcard ($strings)
{
    '*[?]XYZ*' { "Matched: '$_'" }
}

July 7, 2014 at 5:31 am

If the problem is, that the shell takes "?" as a wildcard, but you want it to be a literal "?", you can use the escape character, which is a bactick, just before the "?". Like this: `?
This way, the shell wont treat "?" as awildcard.
Is this what you mean?

July 7, 2014 at 9:10 pm

Yes that is what I mean. Thanks for all the hints. I'll try it.