Issue
I got two files.
-> cat firstfile
sample-dev
sample-dev-auth
-> cat secondfile
sample-dev
sample-dev-auth
sample-dev-temp
and then I execute grep -wvf firstfile secondfile
, which I know will give me differences between two files, so I thought output will be sample-dev-temp
, but NO
-> grep -wvf firstfile secondfile
nothing shows up.
And I tried grep -wf
which opposite to -wvf
, I though it would show me
sample-dev
sample-dev-auth
but still No, it's like
-> grep -wf firstfile secondfile
output:
sample-dev
sample-dev-auth
sample-dev-temp
it returned all items and highlighted the similar part.
why it's working like this? And how to get the result I want?
Solution
The command grep -wf firstfile secondfile
reads pattern from firstfile
and searches for them in secondfile
. When I execute grep -wf firstfile secondfile
I get the following output:
sample-dev
sample-dev-auth
sample-dev-temp
Please note that my output differs from yours. Let me explain the behaviour of grep with my output.
The pattern found in secondfile
(marked as bold) are all listed in firstfile
. This is what grep
should be used for, searching for pattern.
If we add the parameter -v
to the grep command the result will be inverted (by line). Which means that all lines will be displayed which do not have any match. Therefore we have an empty result because all lines match. We can verify this behaviour by adding additional data to secondfile
, e.g.
$ echo hello >> secondfile
$ grep -wvf firstfile secondfile
hello
Only hello is displayed because no pattern in firstfile
matches here.
Having said that grep
is not the tool you should use in this scenario. You can use the diff
tool which is for finding differences in files, e.g.:
$ diff firstfile secondfile
2a3
> sample-dev-temp
Please have a look at the manual of diff
. There are many options for customization.
Answered By - Martin Garbe Answer Checked By - Marilyn (WPSolving Volunteer)