Search code examples
unixawksedcygwin

Remove data in file1 against file2


This might be the worst example ever given on StackOverflow, but my purpose is to remove everything in File1 against File2. Whilst ignoring case sensitivity and matching the entire line. For example Cats@123:bob would be removed from File2 as the word Cat appears in File1. So regardless of case sensitivty, if a matching word is found it should eradicate the entirety of the line.

Input (File1):

Cat
Dog
Horse
Wheel

MainFile (File2)

Cats@123:bob
dog@1:truth
Horse-1:fairytale
Wheel:tremendous
Divination:maximus

Desired output

Divination:maximus

As the output shows, only "Divination:maximus" should be outputted as no matching words were found in File1. I prefer to use Sed or Awk generally as I use Cygwin. But any suggestions are welcomed, I can answer all questions you may have, thanks.

Here's what I've tried so far, but it's not working unfortunately, as my output is incorrect. To add to this, simply the wrong lines are being outputted. I'm fairly inexperienced so I don't know how to develop upon this syntax below, and maybe it's completely irrelevant to the job at hand.

grep -avf file1.txt file2.txt > output.txt

Solution

  • The grep command can do that for you:

    grep -v -i -f file1 file2
    
    • The -f file1 tells grep to use the patterns in file1
    • The -i flag means case insensitive
    • The -v flag means to search lines that do not contain those patterns