Search code examples
cmdfindstr

Use Windows CMD findstr to match lines with two words separated by one or more spaces


For the file sub.txt

callfoo()     # no match
call_foo()    # no match
call foo()    # match
call  foo()   # match

the command

findstr call.*foo sub.txt

matches all lines. How can I match the last two lines, where "call" and "foo" are separated by one or more spaces and no other characters? Trying to use regular expressions with

findstr /R call\s.*foo sub.txt

does not match anything.


Solution

  • findstr /RC:"call  *foo" sub.txt
    

    find call followed by a space, followed by zero or more spaces, followed by foo.

    The usage of the /c switch is essential to process the spaces as characters and not as separators.

    \s is not part of the crippled findstr REGEX.