0

I have not found any commandline tool which would print output only if it matches given wildcards. Wildcards * and ? needs to be supported.

For example

echo 192.168.1.1 | WildcardMatch "19?.168.*"
echo aa-02-23201 | WildcardMatch "*-??-232*"

Tried to implement this by using a batch file and calling PowerShell but this is way too slow. https://support.microsoft.com/en-us/office/using-the-like-operator-and-wildcard-characters-in-string-comparisons-c472b6df-0d3e-4ffc-a21c-bb5721cc460a

Edit: -i is needed to make the match case-sensitive.

2
  • This feels like an XY Problem. What is it you're actually trying to achieve?
    – Tetsujin
    Commented Apr 11, 2023 at 12:38
  • I have files with thousand of lines and a filters containing ? and * characters and only matching lines should be processed further.
    – JPX
    Commented Apr 11, 2023 at 13:37

3 Answers 3

1

Here is an examaple of how to use PowerShell which supports ? and *.

https://github.com/Tataz2/likeSTR

PowerShell scripts can be compiled to exes.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 23, 2023 at 12:06
0

I have not found any command line tool which would print output only if it matches given wildcards.

Wildcards * and ? needs to be supported.

There are no command line tools that support ? as a rexexp character that I am aware of.

However you can use findstr to do the match you are looking for as follows:

This will match:

F:\test>echo 192.168.1.1 | findstr /r "[1][9][1-9].168.*"
192.168.1.1

This will not match:

F:\test>echo 182.168.1.1 | findstr /r "[1][9][1-9].168.*"

F:\test>

Further Reading

0

You can also install grep for Windows and use more or less basic regexes.

Examples based on yours, with Perl engine in use, but ou could switch to e.g. faster -G instead:

  • less accurate IP match:

    echo 192.168.1.1 | grep -P "19\d\.168\..*?"

  • more accurate IP match:

    echo 192.168.1.1 | grep -P "19\d\.168\.\d{1,3}\.\d{1,3}"

  • pattern match:

    echo aa-02-23201 | grep -P "[a-z]{2}-\d{2}-232\d+"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .