Findstr: find only words isolated, no as part of string.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JuanMa777
Posts: 17
Joined: 06 Nov 2016 13:15
Location: La Plata, ¡Argentina!

Findstr: find only words isolated, no as part of string.

#1 Post by JuanMa777 » 31 Jan 2017 16:39

Hello,
I'm running a script to find some words, i. e. amor, man, pero, etc., but when I run it find this words even as part as other more long: amor=clamor, man=workman, pero=peronista, etc.
How can I find with findstr this words (amor, man, pero) but isolated, the bordering characters no matter (spaces, _, +, -, etc.), only I find the words isolated: the_man_tall (yes), the man_tall (yes), etc.; the_workman_fast (no), the+workman+fast (no), etc.
I use this command in my script:

Code: Select all

dir /b /s ^| findstr /i /g:keywords.txt

What I need to add?
Thanks!!!
Regards.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Findstr: find only words isolated, no as part of string.

#2 Post by dbenham » 31 Jan 2017 17:28

You should do a regular expression search. FINDSTR has non-standard syntax for anchoring the beginning ( \< ) and end ( \> ) of a word.

keywords.txt

Code: Select all

\<amor\>
\<man\>
\<pero\>


Your existing FINDSTR command will probably work, but it never hurts to explicitly use the /R option for a regular expression search.

Code: Select all

dir /b /s ^| findstr /i /r /g:keywords.txt


Dave Benham

JuanMa777
Posts: 17
Joined: 06 Nov 2016 13:15
Location: La Plata, ¡Argentina!

Re: Findstr: find only words isolated, no as part of string.

#3 Post by JuanMa777 » 31 Jan 2017 19:08

Thanks Dave, your solution works!, but with a few words in keywords.txt. My keywords.txt is so big, and findstr in this mode take a eternity (or perhaps something is wrong).
Maybe the best is back to the old method, unless you or someone has a solution...
We see.
Thanks.

Post Reply