how to subtract the contents of one file from another one's using grep?

Having tried tail for deleting the first two lines of a file, I wanted to try grep to subtract the contents of one file from another one’s. The suggestion was:

grep -vf smallfile bigfile

but it didn’t work. However, the following worked

grep -v -x -f smallfile bigfile

I’d like to know why, man doesn’t give me any clue. I’m using bash on openSUSE 12.3

Because:

**       -x, --line-regexp**
              Select  only  those  matches  that exactly match the whole line.
              (**-x** is specified by POSIX.)

(from “man grep”)

wolfi, I saw that, I read the man. It just doesn’t make sense - it should work without “-x” !!! The first three lines are identical and the first file has only three lines. Why is the use of “-x” necessary ???

Well, to be able to answer that, one would have to see your files…

And you only said it didn’t work without ‘-x’, but not in what respect it didn’t work.

But if you only want to delete the first two lines, why don’t you stay with tail?

It didn’t work without ‘-x’ in the sense that it didn’t subtract the common lines and it didn’t do anything at all. If I add ‘-x’ it subtracts the common content and displays the difference.

Again, how should anybody know why it is not working without seeing the input files?

It works fine here with a test case:

# cat file1
Hello
Test
# cat file2
Hello
Test
It works?
# grep -vf file file2
It works?

wolfi323 wrote:
> It works fine here with a test case:
>
> Code:
> --------------------
> # cat file1
> Hello
> Test
> # cat file2
> Hello
> Test
> It works?
> # grep -vf file file2
> It works?
>
> --------------------

That must be magic, then unless ‘file’ happens to contain the same
content as ‘file1’ :slight_smile:

You’re supposed to cut and paste text in code quotes, not tell us
stories! :slight_smile:

Right, I had called the files xxxxx and xxxxx2, but then decided to edit the filenames here for better readability.
So again, copy and pasted:

wolfi@amiga:~> cat xxxxx
Hello
Test
wolfi@amiga:~> cat xxxxx2
Hello
Test
It works?
wolfi@amiga:~> grep -vf xxxxx xxxxx2
It works?
wolfi@amiga:~>

Do you believe it now? Should I post a screenshot? :wink:

wolfi323 wrote:
> Do you believe it now? :wink:

Oh, I believed you before. Would you lie to me? :slight_smile: