0

I have 2 files and I want to find the difference and output only what is not inside ex2

If use grep -Ff ex2.txt ex1.txt, it shows me a b c d, if I use the same command with -v there is not output. Why with does the first command also output d?

What would be the easiest way to do this? Is grep suitable for this? Maybe diff maybe a script that would read each line and output if it's there or not?

cat ex1.txt
a
b
c
d

cat ex2.txt
a
b
c

Edit---my output shows up like this:

cat ex2.txt
a
b
c

cat ex1.txt
a
b
c
d

grep -vf ex2.txt ex1.txt

grep -f ex2.txt ex1.txt
a
b
c
d
5
  • Is this a homework assignment, by chance? What have you tried so far? Have you actually tried the "diff" command?
    – RVT
    Oct 4, 2016 at 9:37
  • No its not I just want to understand why you also see d on the output if it is supposed to grep whatever is on that file. I tried diff i want to know if you can have the same results with grep, too.
    – Olive.b
    Oct 4, 2016 at 9:42
  • Possibly ex2.txt also contains an empty line which would tell grep to look for the empty string and the empty string can be found in any line. Oct 4, 2016 at 9:48
  • I added a solution which I think might help you learn from this... though, I maintain it's probably not the "right" way to do it.
    – RVT
    Oct 4, 2016 at 9:54
  • @StéphaneChazelas looks like you're right. Have a look at the edited question. There's a blank line at the end of ex1.
    – terdon
    Oct 4, 2016 at 10:56

3 Answers 3

1

grep is not really the right tool for the job here, but a quick look at the grep manpage should help (please note that grep will vary semi-wildly between distributions, as some may use GNU by default and others may use something else).

  • -F ... Fixed Strings (separated by newlines) - (Category: matcher selection)
  • -f ... Filename (Category: matching control)
  • -v ... Invert match (Category: matching control)

So, you might imagine that something like grep -Fvf ex2.txt ex1.txt would work. However, this is not a good solution, as it requires you to already know something about the files prior to starting. There are better tools (like variants of diff) to give you more specific and more accurate differences between files and/or directories.

Edit: Fixed strings, not Fire strings (auto-corrupt be damned)

0

Try to change the order of the files:

grep -v -f ex2.txt ex1.txt
> d
2
  • I believe the OP had "fixed" that prior to your answer, but was using "Ff" rather than "vf" ... I've posted a solution that uses "Fvf" instead. (though, this isn't really the right tool for the job, IMO)
    – RVT
    Oct 4, 2016 at 9:53
  • Considering that the question is "how can I do it with grep", this is an answer that works. However, if it is just about finding the differences, I would use diff too.
    – Soerendip
    Oct 4, 2016 at 9:59
0

Cannot reproduce your error:

cat ex2.txt 
a
b
c

cat ex1.txt 
a
b
c
d

grep -vf ex2.txt ex1.txt 
d

grep -f ex2.txt ex1.txt 
a
b
c

grep -Ff ex2.txt ex1.txt 
a
b
c

grep -vFf ex2.txt ex1.txt 
d

grep -V
grep (GNU grep) 2.20

lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 8.5 (jessie)
Release:    8.5
Codename:   jessie


Edit (on OS X with BSD grep):

cat ex2.txt 
a
b
c

cat ex1.txt 
a
b
c
d

grep -f ex2.txt ex1.txt 
a
b
c

grep -vf ex2.txt ex1.txt 
d

grep -Ff ex2.txt ex1.txt 
a
b
c

grep -v -Ff ex2.txt ex1.txt 
d

grep -V
grep (BSD grep) 2.5.1-FreeBSD
5
  • Not all greps are built alike.
    – RVT
    Oct 4, 2016 at 9:51
  • Strange, I have different output. I edited my initial question with the output.
    – Olive.b
    Oct 4, 2016 at 10:04
  • Indeed... OP hasn't given enough information for a "great" answer, here. But, YMMV depending on implementation (keeping in-mind that grep is often aliased and/or linked to one of the many others on a system, including egrep, fgrep, etc).
    – RVT
    Oct 4, 2016 at 10:13
  • @RVT Oddly enough I just tried this on two different version of grep and I still get the desired output. Which one are you using that does not work? Oct 4, 2016 at 10:15
  • @maulinglawns - I think you've mostly covered the bases, here. But, I'm just saying that "not all greps are built alike" and "OP hasn't given clear enough information" for a definitive answer... that is all. There are some systems that are "strange" out there (eg. Solaris). (And honestly, I'm still not convinced this isn't a homework assignment... LOL)
    – RVT
    Oct 4, 2016 at 10:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .