Look in txt file for string & appending to it

Microsoft Windows xp professional w/ sp2
January 5, 2010 at 12:39:57
Specs: Windows XP
TL;DR:

Before you read below, here is the simple version of what I'm trying to do... if you can give me some resources as to how I can create a script that does just the following, I'll be very happy.

I want to compare text file A with text file B and write any particular LINE that IS in file A but NOT in file B, to a new text file.
NOTE that the important thing is that this is LINE-BY-LINE; The two lists will not be in the same order. :-)
_____________________________________________

Ok, so I'm trying to figure out, conceptually, how to go about creating a batch file to do the following:

1. Look inside a text file of a specific name, in a specific directory, for a specific string.
2. If that string is found, don't replace or remove it, but APPEND to it, the word: "CLEAN"
3. Continue down the line.

The structure of the file should be something similar to:
In c:\file.txt, if exist "pickles pickles pickles", echo (to the end of that line): " ((CLEAN))"
[then...] if exist "oranges oranges oranges" echo (to the end of that line) " ((CLEAN))"
and so on, for each of the things I'd like to check for.

So, let's say we have a text file containing the following information:

apples apples apples
pickles pickles pickles
walnuts walnuts walnuts
oranges oranges oranges

After running the batch file, I would like for it to say something to the tune of:

apples apples apples
pickles pickles pickles ((CLEAN))
walnuts walnuts walnuts
oranges oranges oranges ((CLEAN))

I know, conceptually, what I want to do - the problem is, after researching, I can't figure out how to make it compare the two and append to one.

I'm flexible on how exactly it's done, but my main problem is how to look at a file and append to that same specific line.

An alternative way to do it:
I could write up a "list" of entries which should be marked, and use a file compare utility of some sort (such as fc.exe) to compare the two files, but the problem there is that it doesn't go line-by-line. For example, if I'm looking for the string "pickles pickles pickles", I don't know if it'll be on the 5th line or the 50th.

Does anyone have any thoughts on how I could make this happen?
Either comparing my list to the other file, one line at a time and appending some text to the end of that line when a match is found,
or running down a list of functions, one for each string to search for.


See More: Look in txt file for string & appending to it

Report •


#1
January 5, 2010 at 19:23:42
unless these files are small, i would suggest another platform/language, because batch requires that file2 be scanned from start to finish for each line of file1, even
if they're sorted. This is my shot at batch:

@echo off & SETLOCAL enabledelayedexpansion
for /f "tokens=* delims=" %%a in (list1) do (
echo %%a
set ff=
for /f "tokens=* delims=" %%b in ('findstr /b /e /r /m /c:"%%a" list2') do (
set ff=%%b
)

if "!ff!" equ "" (set xx=!ff! ) else (set xx=[CLEAN])
echo.%%a!xx!>>tempfil
)
goto :eof
::tempfil has your output, rename/move/copy etc...
... v e r y v e r y s l o w ...

QB (translates fairly easy to VB and somewhat vbscript)
(also, not tested since doubtful it will be used...)

SHELL "SORT LIST1 > SLIST1"
SHELL "SORT LIST2 > SLIST2"
OPEN "SLIST1" FOR INPUT AS #1
OPEN "SLIST2" FOR INPUT AS #2
OPEN "TEMPFIL" FOR OUTPUT AS #3
L2$=""
DO WHILE NOT EOF(1)
LINE INPUT #1,L1$
A1:
IF L1$=L2$ THEN PRINT #3,L1$+"(CLEAN)" : GOTO SS
AA:
IF L1$<L2$ THEN PRINT #3,L1$ : GOTO SS
IF EOF(2) THEN GOTO CLSTOP
LINE INPUT #2,L2$
GOTO A1
:SS
LOOP
CLSTOP:
CLOSE : STOP


Report •
Related Solutions


Ask Question