Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I search for multiple strings in one "find" command in batch script?

I have a windows batch script that will look for a string within a file

find /i "WD6"  %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff

Currently this is what my code looks like. I've come across a new string I want to search for in the same file and do the same action if it finds it, it stored it in a variable called %acctg_cyc% can I search for both strings in one line of code? I tried this:

find /i "WD6" %acctg_cyc%  %Inputpath%file.txt
if %errorlevel% == 0 GOTO somestuff

But it seems to ignore the %acctg_cyc% and only look for "WD6" in file.txt. I tried testing where %acctg_cyc% is in file.txt and when it is not and it passes both times.

Any thoughts? I know I could do this in more lines of code but I'm really trying to avoid that right now. Maybe it's just not possible.

Thank you for any help!

like image 645
intA Avatar asked Jul 20 '15 14:07

intA


People also ask

How do I find multiple strings in findstr?

When the search string contains multiple words, separated with spaces, then findstr will return lines that contain either word (OR). A literal search ( /C:string ) will reverse this behaviour and allow searching for a phrase or sentence. A literal search also allow searching for punctuation characters.

What does %% do in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is && in batch?

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0 ). The opposite of && is || , which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0 ).

How do I search for a string in a command prompt?

Search for a text string in a file & display all the lines where it is found. "string" The text string to find (must be in quotes). [pathname] A drive/file(s) to search (wildcards accepted). /V Display all lines NOT containing the specified string. /C Count the number of lines containing the string.


2 Answers

find isn't very powerful. It searches for one string only (even if it is two words): find "my string" file.txt looks for the string my string.

findstr has much more power, but you have to be careful how to use it:

findstr "hello world" file.txt 

finds any line, that contains either hello or world or both of them.

see findstr /? for more info.

Finding both words in one line is possible with (find or findstr):

find "word1" file.txt|find "word2"

finding both words scattered over the file (find or findstr):

find "word1" file.txt && find "word2" file.txt
if %errorlevel%==0 echo file contains both words
like image 172
Stephan Avatar answered Sep 30 '22 11:09

Stephan


I tried findstr with multiple /C: arguments (one for each to be searched sentence) which did the trick in my case. So this is my solution for finding multiple sentences in one file and redirect the output:

findstr /C:"the first search" /C:" a second search " /C:"and another" sourcefile.txt > results.txt
like image 32
Piemol Avatar answered Sep 30 '22 12:09

Piemol