computing
  • 3

Batch File To Delete Specific Words In Txt

  • 3

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!!!

Share

1 Answer

  1. 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.

    • 0