Find match in specific token from list

Microsoft Windows 7 professional 64-bit
February 2, 2011 at 02:36:38
Specs: Windows 7
I have lines of code in "animals.txt" that look like this:

my file (A)¬cat¬¬c:\animals\cats¬blah¬blah¬¬
my file (B)¬dog¬cat¬C:\animals\doggies¬blah¬blah¬¬
my file (C)¬hamster¬dog¬C:\animals\hamsters¬blah¬blah

I have a text file "text.txt" which contains
cat
dog
bird

I want to produce a text file as an output that looks for the terms from Test.txt in token two of animals.txt only, and if they match to output the whole line into 'pets.txt' ie:

my file (A)¬cat¬¬c:\animals\cats¬blah¬blah¬¬
my file (B)¬dog¬cat¬C:\animals\doggies¬blah¬blah¬¬

has matched cat and dog respectively

this matches all occurances within the file, not just token two
findstr /g:text.txt animals.txt >> pets.txt

this matches token two only, but will only put token two into pets.txt, not the whole line:
for /f "tokens=2 delims=¬" %%a in ('findstr /g:text.txt animals.txt') do >> pets.txt echo %%a

I cannot work out how to output the whole line

Once this is done I may end up with more than one entry where cats is in token 2. I wish to be alerted to those so that I can manually choose which line to keep

thanks


See More: Find match in specific token from list

Report •


#1
February 2, 2011 at 03:27:07
You file has high-bit chars. AAhex at positions 12, 16, 17...

Might work but check to see if that's what's really in the file or did it get mangled by the forum.


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
February 2, 2011 at 04:09:55
thanks - the delimiters in the file really are: ¬
hence delims=¬ in my attempted for /f loop

Report •

#3
February 2, 2011 at 04:35:41
:: ==========================================
::
:: butter.bat Wed 02-02-2011 18:17:53.61
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%f in (animals.txt) do (
for /f "tokens=2 delims=¬" %%a in ("%%f") do (
for /f "tokens=* delims= " %%i in (text.txt) do (
if "%%i" equ "%%a" (
>> newfile echo.%%f
)
)
)
)


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
February 2, 2011 at 07:18:25
amazing - thank you so much for the hours of my life you've just given me. Flawless. Also your reply does confirm that this would have been beyond me

Regarding the second part of my question: I'm actually going to use this on multiple animals.txt hence I would get more than one match for each line - however I have a clear order of preference for each file (ie: I know the dog from which file I'd prefer)

Easiest would actually be if I simply get a nonmatches of those filenames from text.txt which don't match, rather than multiple matches at the end of the whole process: I could then use this as text2.txt against animals2.txt i.e.: text2.txt/nonmatches contains:

bird

I've tried adding this at the bottom of your script, but the \V will only find non-matches in matches.txt rather than text.txt, and anyway I worry that getting it the right way round would cause findstr an out of memory error:

for /f "tokens=2 delims=¬ %%j in ('findstr /V /G:text.txt matches') do >> nonmatches echo %%j

I've also tried the obvious of modding your script for NEQ but I think this would try outputting every single non-match made in the search, rather than the animal names from text.txt which don't find a match

thanks again


Report •

#5
February 3, 2011 at 02:58:04
"I know the dog from which file I'd prefer"

how?


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
February 3, 2011 at 04:36:02
thanks M2, much appreciated - the nature of my files is that I have animals1.txt which contains the best data, animals2.txt which contains slightly less preferred data, and so on....

I have realised my findstr process for achiveing this as described above would work better to create nonmatches.txt if I first create a list of just token2 from matches and then findstr \V that against text.txt in the for loop. However when I insert that form into the above for /f (findstr \V) syntax, it seems to still only find matches, as if findstr is ignoring the \V. That's the stage I'm at right now....


Report •

#7
February 4, 2011 at 00:53:02
OK I think we can survive this.

If your file names are :

animals1.txt
animals2.txt
animals3.txt
...


ie 1 ~ 9, we're OK. But if there's a 10 it will get ahead of 2.

Please confirm the range of file names.


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
February 4, 2011 at 01:48:53
I am very fortunate that I can rename the animals.txt to anything I want, and after about 4 of them I will have all the matches I'm going to get

Just to show I'm not sitting back here I did manage to do what I said:

@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%f in (animals.txt) do (
for /f "tokens=2 delims=¬" %%a in ("%%f") do (
for /f "tokens=* delims= " %%i in (text.txt) do (
if "%%i" equ "%%a" (
>> newfile echo.%%f
)
)
)
)
@echo on > namedmatches.txt
@echo on > nonmatches.txt

for /f "tokens=2 delims=¬" %%a in (newfile) do (
>> namedmatches.txt echo.%%a
)

for /f "tokens=* delims=" %%b in (
'findstr /VX /g:namedmatches.txt text.txt') do (
>> nonmatches.txt echo %%b
)

del namedmatches.txt
exit

will give me both my list in newfile as before, but also a nonmatches.txt - I could then use nonmatches.txt in place of text.txt and repeat the process for animals2.txt

so panic over, but naturally i'm sure you can do a lot better/neater than this, so I'd be delighted with any further input you have time to give me


Report •


Ask Question