Find in files: Find all files matching term1 AND term2

Find, replace, find in files, replace in files, regular expressions

Find in files: Find all files matching term1 AND term2

Postby HansFink » Fri Jan 27, 2006 12:53 pm

Hello,

I want to search multiple files and list those who match both search terms.

Example:
I want to search for "John" and "Smith" in multiple files. Now there are 4 possibilites:

- contains neither John nor Smith
- contains John
- contains Smith
- contains John & Smith

I just want the files from the last category.

Is that possible?
User avatar
HansFink
Basic User
Basic User
 
Posts: 17
Joined: Fri Jan 27, 2006 12:00 am

Re: Find in files: Find all files matching term1 AND term2

Postby Mofi » Fri Jan 27, 2006 2:44 pm

I don't know, if there is a solution with a single regular expression search. Maybe, it depends on the structure of your files. You have forgotten to give us some examples which is unfortunately typical for new forum users.

So I have quickly developed a macro which is able to do this indepent of the file structure.

The first part searches for "John". You can modify the first FindInFiles command to your needs (open files or specified files in a directory, regexp, ...), but it must write the result to a log file. This macro is taken from viewtopic.php?t=2467

The second part is taken from viewtopic.php?t=493
It closes all open files except the search result file.

The third part I have developed new. It opens all files from the first search. So after the 3rd loop all files are opened which contain the word "John".

The last part is simply a Find In All Open Files for "Smith". You can modify all parameters of this FindInFiles command, but it must run on all open files.

InsertMode
ColumnModeOff
HexOff
UnixReOff
FindInFiles Log OpenFiles "" "" "John"
Loop
Find RegExp Up "%Search complete, found *^p"
IfFound
Delete
ExitLoop
Else
NextWindow
EndIf
EndLoop
Top
Find "----------------------------------------^p"
Replace All ""
Find RegExp "%Find *^p"
Replace All ""
Find RegExp "%Found *^p"
Replace All ""
Find RegExp "/[0-9]+:*$"
Replace All ""
SortAsc IgnoreCase RemoveDup 1 -1 0 0 0 0 0 0

Top
"ThisIsTheSearchResultWindow"
Loop
NextWindow
Top
Find MatchCase "ThisIsTheSearchResultWindow"
IfFound
Delete
ExitLoop
Else
CloseFile
EndLoop

Clipboard 9
Loop
IfEof
CloseFile NoSave
ExitLoop
Else
StartSelect
Key END
Cut
Key DEL
EndSelect
Open "^c"
NextWindow
EndIf
EndLoop
ClearClipboard
Clipboard 0
FindInFiles OutputWin OpenFiles "" "" "Smith"
UnixReOn

Remove the last red command, if you use regular expression in UltraEdit style by default instead of Unix style.
For UltraEdit v11.10c and lower see Advanced - Configuration - Find - Unix style Regular Expressions.
For UltraEdit v11.20 and higher see Advanced - Configuration - Searching - Unix style Regular Expressions.
Macro commands UnixReOn/UnixReOff modifies this setting.

And don't forget to enable the macro property Continue if a Find with Replace not found.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions