FFIND wordA AND wordB

Aug 3, 2016
I "have to" search through a lot of textfiles to find out if they contain somewhere WordA .and. WordB (in any order).

The most straightforward way would be: List all the files that contain WordA and search through all those files to see if they contain WordB.
But as there are really a lot of textfiles (an offline version of Wikipedia) and also more words to look for ... saving on iterations is quite welcome.

I *think* I tried all (combinations of) options of FFIND, including the regular expressions, but did not succeed.
Is it possible with FFIND? Or should I better try this with programs like AWK or SED? (I used those a couple of times and I did survive ;-)
Might FINDSTR be of assistance, for example
The following are all equivalent ways of expressing a case insensitive regex search for any line that contains both "hello" and "goodbye" in any order

/i /r /c:"hello.*goodbye" /c:"goodbye.*hello"
-i -r -c:"hello.*goodbye" /c:"goodbye.*hello"
/irc:"hello.*goodbye" /c:"goodbye.*hello"

...or one of the other many options available for FINDSTR.

Ref: SS64 FindStr

May 20, 2008
Syracuse, NY, USA
I "have to" search through a lot of textfiles to find out if they contain somewhere WordA .and. WordB (in any order).
... not necessarily in the same line? ... not with a line-based text util. But if you don't mind two FFINDs ... this finds all CPPs containing "wmain" and "goto" in either order. Use regexes if you want whole words, case, et c. There are 734 CPP files in P:. This took about 4 seconds.
v:\> do f in /p ffind /b /s /l /m /t"wmain" p:\*.cpp ( ffind /b /l /m /t"goto" %f )

... in the same line? Do that with TPIPE's grep and a regex like "wordA.*wordB|wordB.*wordA".
AWK (or PERL, my tool of choice) are considerably faster for heavy text processing.

In Perl, a basic script would look like this [untested]:
my $fn = $ARGV[0] ;
open my $fh, "<", $fn or die $!;
local $/; # enable localized slurp mode
my $content = <$fh>;
if ( $content =~ m/WordA/ && $content =~ m/WordB/ ) { print "$fn\n" ; }
close $fh;
  • Like
Reactions: MaartenG
Aug 3, 2016
@Joe Caverly : Vince is right; FINDSTR is line oriented and the text could be anywhere on the page. If you use FINDSTR more often (as I do; it is not the most sophisticated utility, but it is on every Windows system), you might like this page: [title] where someone did some extensive research on the (undocumented) options of FINDSTR.

@vefatica : Nice one-liner, but still requires a 2-pass reading of the files. Something I like to avoid if possible.
But it gave me inspiration for another possibility: Read the textfile as an array and parse that one. Will work on that.

@Christian Albaret : Not in a million years I would have thought about Perl. It just wasn't on my radar (it's one of those things that you pass 4 times a day without really noticing). It seems like a really strong option. Thanks a lot for pointing out!
It is time to (finally) learn som Perl :-) And your script will surely help with that. Thanks!
Similar threads
Thread starter Title Forum Replies Date
J ffind does not find files Support 4
D ffind hangs on large file Support 18
Alpengreis ffind dialog (/W) problem Support 4
vefatica FFIND, temp files? ... not deleted? Support 0
S FFIND text that includes " Support 7
D ffind /e hangs Support 7
vefatica FFIND needs work Support 12
Joe Caverly Multiple Text Searches at once using FFIND or TPIPE Support 4
Kachupp FFIND TPIPE Support 9
Gamegod ffind bug with chinese Support 2
vefatica FFIND and multi-line regular expressions Support 4
vefatica Make FFIND a bit more friendly? Support 14
M Trying to use ffind with a @file.lst doesn't work Support 4
vefatica Corruption from FFIND? Support 5
vefatica FFIND /S and System32? (and an OT mystery) Support 14
T FFind - can we display n number of lines after the find? Support 2
vefatica Help nit (FFIND and DIR with /S) Support 0
R ffind not setting %_ffind_ vars if /f is used Support 7
vefatica FFIND and _? Support 1
B Fixed FFIND doesn't find last character in file without CR/LF at EOF Support 1
dcantor How to make line numbers fixed width in FFIND Support 11
vefatica FFIND /S, find directory with specified name? Support 7
D Why doesn't ffind find directory? Support 3
C FFind /w works but FFind /= doesn't? Support 2
vefatica FFIND goes crazy Support 8
Stefano Piccardi TCC 13 vs. TCC 14 different FFIND /E"d$" output Support 1
Steve Pitts How to? FFIND, regular expressions and double quotes Support 4
vefatica FFIND /S in my profile directory Support 2
A How to? Pass output from ffind to another command in a BTM file Support 5
Phileosophos Can FFIND match file names with a regular expression? Support 19
vefatica FFIND and size ranges Support 1
R How to use ffind to find older files? Support 9
J How do I - ffind - with multiple items Support 10
M FFIND and LIST does not work for UTF-8 fles Support 10
L FFIND no result display Support 10
H Directory wildcards not working with ffind Support 3
D ffind and directories Support 2
Stefano Piccardi detecting BOM, FFIND multibyte regex Support 18
dcantor FFIND syntax -- is /E"regex" /X supported? Support 2
S FFIND and quoted strings Support 4
S FFIND /E"reg exp" : not case insensitive Support 4
J Problem with FFIND Support 1

Similar threads