Batch file to delete specific words in txt

February 9, 2010 at 06:48:29
Specs: Windows XP, Centrino 2 vPro
Example, I have a text file and the contents are:

Dog Cat Horse 3456
Horse Dog Mouse 1234
Bird Cat Dog 4567
Dog Mouse Bird 1112
Horse Cow Fly 22234
Cow Dog Mouse 54444
Cow Fly Mouse 2323222

How do I write a batch file that will remove any line that has "Dog Mouse" and "Cow Fly" but it has to be exact match so I don't want it to remove a line that might have "Dog" in it somewhere or "Cow" in it somewhere, etc.

I was able to use the Find command to delete specific single words but not the exact match of two words or more as they are placed in the sentence (e.g. "Dog Mouse")

Hope that makes sense.

Thanks!!!



See More: Batch file to delete specific words in txt

Report •


#1
February 9, 2010 at 07:26:31
If you want to match the strings of two words wherever they are placed in the line (i.e. "Dog Mouse Bird 1112" or "Cow Dog Mouse 54444") assuming the words are separated by just one space the following does the job

type file.txt | find /V "Dog Mouse" | find /V "Cow Fly" > file.new

Otherwise post again explaining better your needs.

Report •

#2
February 9, 2010 at 07:40:26
I beleive that does it!!! I could've swore I tried something similar to it but oh well. Thank you so much!

Report •

#3
February 9, 2010 at 08:18:54
Actually, one more thing... :)

Is there a way to ignore duplicate lines and just keep one line of the lines so:

Dog Cat Horse 3456
Horse Dog Mouse 1234
Bird Cat Dog 4567
Dog Mouse Bird 1112
Dog Mouse Bird 1112
Dog Mouse Bird 1112
Horse Cow Fly 22234
Cow Dog Mouse 54444
Cow Fly Mouse 2323222
Cow Fly Mouse 2323222
Cow Fly Mouse 2323222

I want to filter any duplicates and have the following result:

Dog Cat Horse 3456
Horse Dog Mouse 1234
Bird Cat Dog 4567
Dog Mouse Bird 1112
Horse Cow Fly 22234
Cow Dog Mouse 54444
Cow Fly Mouse 2323222

Thanks again!!!


Report •

Related Solutions

#4
February 9, 2010 at 09:08:48
If the duplicate lines are grouped in bursts the following applies...

@echo off & setlocal EnableDelayedExpansion

set /P row=< file.txt
echo.%row%> file.new

for /F "skip=1 delims=" %%j in (file.txt) do (
  if not "%%j"=="!row!" echo.%%j>> file.new
  set row=%%j 
)

Otherwise the source file must be previously sorted, but the native order is lost.

Report •

#5
February 9, 2010 at 10:21:45
Hmm.. it wont seem to work for me. Could you provide small amount of original and output text so I can see how the results worked out? Or can you maybe use my provided example text with this somehow?

Report •

#6
February 9, 2010 at 12:41:13
Sorry, the script was untested and an extra space caused the bug. Please replace

set row=%%j

with

(set row=%%j)

and the batch should work fine.


Report •

#7
February 9, 2010 at 13:43:54
Excellent! Thanks a lot!!

Report •

#8
February 10, 2010 at 06:01:02
I was trying this, but the SORT command does not seem to allow duplicate lines to be removed.

find /V "Dog Mouse" file.txt | find /V "Cow Fly" | sort > file.new

Anyway, be aware of the SORT command, it may help you


Report •


Ask Question