search list of strings in a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

search list of strings in a file

#1 Post by ladduq » 05 Mar 2013 02:22

Hi


I have a file which contains a list of keywords up to 300.
These keywords are to be searched one by one in another
text file which has the matching keyword lines and output
the matching lines in another file.

I have made it upto this but unable to get output.pls suggest

@echo off
cls
set info=The_file_in_which_keywords_are_tobe_searched_and_output_in_resultsfile.txt
set results=results.txt
FOR /F "tokens=*" %%1 IN ('findstr /G:search_list_file_which_contains_keywords_or_search_strings.txt %info%') DO echo %%1 >>%results%

Thanks

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: search list of strings in a file

#2 Post by mfm4aa » 05 Mar 2013 03:06

Your code is working for me. What kind of issue do you have?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search list of strings in a file

#3 Post by foxidrive » 05 Mar 2013 04:28

ladduq wrote:FOR /F "tokens=*" %%1 IN ('findstr /G:search_list_file_which_contains_keywords_or_search_strings.txt %info%') DO echo %%1 >>%results%



Avoid using numerals. Use alpha characters.

ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Re: search list of strings in a file

#4 Post by ladduq » 05 Mar 2013 07:35

mfm4aa wrote:Your code is working for me. What kind of issue do you have?



yes i also tested the code but only for about 10 keywords i tested and it was working fine.

However when i implemented the same for 300 keywords it is not working.It is not displaying the output in results file.But when i manually find in the file there were instances matching the keywords.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search list of strings in a file

#5 Post by foxidrive » 05 Mar 2013 07:52

Show us a scenario where it fails. Try the keywords and file data and see if it fails. Post them here.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: search list of strings in a file

#6 Post by mfm4aa » 05 Mar 2013 08:20

There are some FINDSTR issues, e.g.
Recommendation - Always explicitly specify /L literal option or /R regular expression option when using "string argument" or /G:file.

http://stackoverflow.com/questions/8844 ... 73#8844873

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search list of strings in a file

#7 Post by foxidrive » 05 Mar 2013 08:24

Case sensitive searches is also a trap at times. The /I switch is often necessary.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search list of strings in a file

#8 Post by foxidrive » 05 Mar 2013 09:31

mfm4aa wrote:There are some FINDSTR issues, e.g.
Recommendation - Always explicitly specify /L literal option or /R regular expression option when using "string argument" or /G:file.

http://stackoverflow.com/questions/8844 ... 73#8844873



I found that in another thread using exclusions, and it seems that even if I specify /r the line isn't treated as a regexp if it starts with plain text.

As a test I had this in the exclusions.txt and it excluded out-000001.txt where it should only exclude outtttt and any number of t characters, if it was treated as a regexp. It was treated as a normal msdos wildcard.

out*

Post Reply