Results 1 to 9 of 9

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

  1. #1
    Join Date
    Feb 2011
    Location
    127.0.0.1
    Posts
    472

    Default 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:
    Code:
    grep -vf smallfile bigfile
    but it didn't work. However, the following worked
    Code:
    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
    Reliable + rider = RiDeR PlUs
    ...if you find this helpful, click on the star below...
    +-+ +-+
    |r| |+|
    +-+ +-+

  2. #2

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

    Quote Originally Posted by riderplus View Post
    I'd like to know why, man doesn't give me any clue. I'm using bash on openSUSE 12.3
    Because:
    Code:
           -x, --line-regexp
                  Select  only  those  matches  that exactly match the whole line.
                  (-x is specified by POSIX.)
    (from "man grep")

  3. #3
    Join Date
    Feb 2011
    Location
    127.0.0.1
    Posts
    472

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

    Quote Originally Posted by wolfi323 View Post
    Because:
    Code:
           -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 ???
    Reliable + rider = RiDeR PlUs
    ...if you find this helpful, click on the star below...
    +-+ +-+
    |r| |+|
    +-+ +-+

  4. #4

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

    Quote Originally Posted by riderplus View Post
    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?

  5. #5
    Join Date
    Feb 2011
    Location
    127.0.0.1
    Posts
    472

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

    Quote Originally Posted by wolfi323 View Post
    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.
    Reliable + rider = RiDeR PlUs
    ...if you find this helpful, click on the star below...
    +-+ +-+
    |r| |+|
    +-+ +-+

  6. #6

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

    Quote Originally Posted by riderplus View Post
    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:
    Code:
    # cat file1
    Hello
    Test
    # cat file2
    Hello
    Test
    It works?
    # grep -vf file file2
    It works?

  7. #7

    Default Re: how to subtract the contents of one file from another one's usinggrep?

    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'

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

  8. #8

    Default Re: how to subtract the contents of one file from another one's usinggrep?

    Quote Originally Posted by djh-novell View Post
    That must be magic, then unless 'file' happens to contain the same
    content as 'file1'

    You're supposed to cut and paste text in code quotes, not tell us
    stories!
    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:
    Code:
    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?

  9. #9

    Default Re: how to subtract the contents of one file from another one's usinggrep?

    wolfi323 wrote:
    > Do you believe it now?


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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •