1

I want to remove all lines in a file containing a string from another file. So I don't want to remove the lines which are identical, there are none, but remove all lines if they share a string from another file. And write the lines which didn't contain a string from the other file to a new file. Example:

File A:

bird, snake
dog, cat
rabbit,fox
eagle,dove

File B:

dog
dove

RESULT File:

bird,snake
rabbit,fox

Please help.

Thanks a lot.

1
  • The best method to use depends on the sizes of both files. Are both files (potentially) (tens of) thousands of lines, or only file A? Apr 14, 2015 at 6:26

1 Answer 1

6

Use the following grep expression:

grep -vFf file_B file_A

Here is a test:

$ cat file_A 
bird, snake
dog, cat
rabbit,fox
eagle,dove

$ cat file_B 
dog
dove

$ grep -vFf file_B file_A 
bird, snake
rabbit,fox
  • -f will read the patterns from a file (one per line), file_B in this case
  • -F will consider the patterns read from file_B as fixed string, meaning no further pattern operation will be done on the patterns
  • -v will print the non-matching lines i.e. the lines of file_A that does not contain the petterns from file_B.
5
  • Hi. Thanks for the answer. In that test it is working. But if I test it with bigger files, for example 20k lines it is not working correctly. Its not removing every line it should do. Maybe half of them or less. Whats wrong?
    – Rick
    Apr 13, 2015 at 20:47
  • Hard to tell without seeing the contents of the actual file....could you provide some actual example lines where this is failing?
    – heemayl
    Apr 13, 2015 at 20:58
  • Just a list of first names for example. With 10 it is working. With 10k not.
    – Rick
    Apr 13, 2015 at 21:09
  • 3
    If its still not working, please edit your question and add some actual file contents from both files where this is not working..
    – heemayl
    Apr 13, 2015 at 23:01
  • @JacobVlijm Let us continue this discussion in chat.
    – muru
    Apr 14, 2015 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.