1

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

1 Answers1

2

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
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • Hey appreciate your reply, and the output was indeed correct. But I noticed I had to manually switch the file to Unix EOL. Is there a way to implement this into the syntax/code/command or whatever the word is :D Update: Don't worry I think I figured it out, thanks for your help heavily appreciated. And thanks once again to Ed Morton for the continuous help :) – StackStackAndStack Jul 20 '19 at 04:03
  • I don't understand your comment. Perhaps you can explain it to me differently? By the way, I am running Linux, so I don't know much about Windows line-ending problems; but please explain. – Hai Vu Jul 20 '19 at 04:10
  • So when I originally did your command, the output was incorrect. I checked the end of line in Notepad++ and it was defaulted to Windows. I switched it to Unix, re-ran the command and the output was perfect. I'm pretty sure: tr -d '\r' needs to be implemented for it to make Unix EOL default (which isn't a big problem AFAIK). – StackStackAndStack Jul 20 '19 at 04:14
  • @StackStackAndStack see https://stackoverflow.com/a/45772568/1745001 for how to really handle DOS line endings. It doesn't involve `tr`. – Ed Morton Jul 20 '19 at 04:44
  • The `dos2unix` aka `d2u` command converts windows line endings to Unix line endings. – Doug Henderson Jul 20 '19 at 23:03