Delete line in txt file based on token
I have txt file full_add where I need to find and delete lines based on tokens in full_delete. Then, save full_add with the rest of the lines. This is sample dat开发者_运维知识库a in each file:
full_add
Employee,800751,,1,9999,,,,,
Employee,800752,,1,9999,,,,,
Employee,800761,,1,9999,,,,,
Employee,800762,,1,9999,,,,,
full_delete
Employee,800751
Employee,800762
Employee,800742
final result in full_add after deliting
Employee,800752,,1,9999,,,,,
Employee,800761,,1,9999,,,,,
This is my idea:
FOR /F "eol=; tokens=2 delims=," %i in (full_delete.txt) do find /v "%i" full_add.txt > full_add.txt
I get empty file full_add. If I change >full_add.txt
for >tmp.txt
, it works but only delete the last token line found. Thanks. Frank
I think you should stay with your current change where you are outputting to tmp.txt
, but in that case you will additionally need to remove the original full_add.txt
and rename tmp.txt
to full_add.txt
:
FOR /F "eol=; tokens=2 delims=," %i IN (full_delete.txt) DO (FIND /v "%i" < full_add.txt > tmp.txt & DEL full_add.txt & RENAME tmp.txt full_add.txt)
Or, instead, you can copy tmp.txt
to full_add.txt
, then remove tmp.txt
:
FOR /F "eol=; tokens=2 delims=," %i IN (full_delete.txt) DO (FIND /v "%i" < full_add.txt > tmp.txt & COPY tmp.txt full_add.txt & DEL tmp.txt)
UPDATE
I changed both FIND commands by adding <
before full_add.txt
.
Also, if you'd like to use the script in a batch file you should probably put the loop body commands on separate lines, like this (using the second script for an example):
FOR /F "eol=; tokens=2 delims=," %i IN (full_delete.txt) DO (
FIND /v "%i" < full_add.txt > tmp.txt
COPY tmp.txt full_add.txt
DEL tmp.txt
)
The indentation is entirely optional, of course.
精彩评论