1.

Solve : findstr Syntax?

Answer»

In:

Code: [Select]set var=born
echo %var% | findstr /i "or">nul
echo %errorlevel%

The errorlevel echoed is 0, presumably because 'born' contains an 'or'. How do I write the findstr line, so that only exact matches to the full word result in no error?

I have also tried:

Code: [Select]echo %var% | findstr /l /i "or">nul

and

Code: [Select]echo %var% | findstr /i /c:"or">nul
In normal English text the word "or" is both preceded and followed by a space.
  Yes it is. Quote from: gregory on December 13, 2010, 06:59:38 AM

Yes it is.

therefore

Code: [Select]echo %var% | findstr /l /i " or ">nulStill GIVES errorlevel 0 for any word containing 'or'.

Perhaps I EXPLAINED it poorly. Any word that is not exactly 'or', should fail to match and cause an errorlevel of 1. Currently any longer word which contains an 'or' e.g. orion, born, will not set the errorlevel.Seems to work with the /C: switch

Code: [Select]echo off
set var=a star called Orion
echo var=%var%
echo %var% | findstr /i /C:" or ">nul
echo errorlevel=%errorlevel%
echo.
set var=The picture of DORIAN Gray
echo var=%var%
echo %var% | findstr /i /C:" or ">nul
echo errorlevel=%errorlevel%
echo.
set var=red or white
echo var=%var%
echo %var% | findstr /i /C:" or ">nul
echo errorlevel=%errorlevel%
Code: [Select]var=a star called Orion
errorlevel=1

var=The picture of Dorian Gray
errorlevel=1

var=red or white
errorlevel=0
My idea of spaces was a step in the right direction. This is the recommended method from the findstr page at ss64.com.

Finding a string only if surrounded by the standard delimiters (comma , semicolon ; equals = space  tab) therefore find the whole word "or" but not the characters o followed by r in e.g. corn, born, risorgimento, forlorn...

put \< before the search string and \> after it

using or as the search string...

echo %var% | findstr /i "\<or\>"

See here

http://ss64.com/nt/findstr.html

This is a useful site to bookmark.

Thanks a whole bunch guy. I'm very appreciative. That seems to do the trick.

The \< and \> are shown in the help for findstr under the 'Regular expression QUICK reference' section. Just by looking, I would think I needed the /r switch....but you don't.

Anywho, thanks again.


Discussion

No Comment Found