matching any part of line in new log csv

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aisha
Posts: 26
Joined: 28 Sep 2016 06:40

matching any part of line in new log csv

#1 Post by aisha » 28 Sep 2016 06:59

please needing help
I have many text files with lots of lines in them (such as sample.txt)
We have file that has keywords strings (such as match.txt)
Trying to make batch that creates a csv log with matching lines (called log.csv), showing the filename and comma and the whole line that matched any part of the text
I have been trying findstr and fart and sfk

SAMPLE TEXT FILE WITH LINES (filename is "sample.txt")
O say can you see by the dawn's early light
What so proudly we hailed at the twilight's last gleaming
Whose broad stripes and bright stars through the perilous fight
O'er the ramparts we watched, were so gallantly streaming?
And the rockets' red glare the bombs bursting in air
Gave proof through the night that our flag was still there
O say does that star-spangled banner yet wave
O'er the land of the free and the home of the brave

MATCH FILE OF LINES WE WANT TO KNOW ABOUT (filename is "match.txt")
can you see, by the dawn's
Whose broad stripes
bursting in air

So if line in sample.txt has any part of something in the match file match.txt then the whole line goes into the log file called log.csv like this
Notice the filename and then a comma and then the whole line, so we can make csv from it:

sample.txt,O say can you see by the dawn's early light
sample.txt,Whose broad stripes and bright stars through the perilous fight
sample.txt,And the rockets' red glare the bombs bursting in air


I hope this question makes sense to you

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: matching any part of line in new log csv

#2 Post by Squashman » 28 Sep 2016 07:05

You are now the 3rd user that has posted from the same ip address in the past few days.

If you are the same person, please stop making new user names.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: matching any part of line in new log csv

#3 Post by Squashman » 28 Sep 2016 07:13

aisha wrote:MATCH FILE OF LINES WE WANT TO KNOW ABOUT (filename is "match.txt")
can you see, by the dawn's

This will not match because it has a comma in the match file but there is not a comma in your sample.txt file.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: matching any part of line in new log csv

#4 Post by Squashman » 28 Sep 2016 07:23

I changed match.txt to match.log because if match.txt exists in the same directory as your files it will match itself. I also quote surrounded each field because it looks like your data may have commas in it. Quote surrounding will protect this if you are opening the file in Excel.

Might be some other caveats that I am not thinking of. But this should get you what you want based on your example.

Code: Select all

@echo off

(FOR /F "tokens=1* delims=:" %%G in ('findstr /G:match.log *.txt') DO (
   echo "%%G","%%H")
)>log.csv

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: matching any part of line in new log csv

#5 Post by aisha » 28 Sep 2016 08:26

Yes that made it

on the same IP yes we are two working together on the same team so you have same IP

again my thanks

Post Reply