Issue
I ran the following command:
grep -f fileA.txt -v fileB.txt
Let's say in fileB there is a string like this: astringlikethis12345
Because I wanted to see what is in fileB but not in fileA, but it returns no hits. but if i do the same command with -w:
grep -f fileA.txt -v -w fileB.txt
it returns the string I was looking for, why is this? -w is to match the whole string, but even without it, it should still return the hit, right? Whether partial or full.
Thank you
Solution
check in fileA for the word you want to exclude in searching of fileA. There is might be a space or tab next to that word .
It is working fine as expected on my system
from grep help :
-w, --word-regexp force PATTERN to match only whole words
-v, --invert-match select non-matching lines
[root@project1-master ~]# cat filea
this
[root@project1-master ~]# cat fileawithspacenexttothis
this
[root@project1-master ~]# cat fileb
this
that
these
thisthis
this this that
- "grep -f filea -v fileb" is equivalent to "grep -v this fileb" , so it returns that & these
[root@project1-master ~]# grep -f filea -v fileb that
these
2."grep -f filea -v -w fileb" is equivalent to grep -v -w this fileb , so it will display 'thisthis' as well as there is word boundary for this in 'thisthis'
[root@project1-master ~]# grep -f filea -v -w fileb
that
these
thisthis
- grep -f fileawithspacenexttothis -v fileb is equivalent to "grep -v 'this ' fileb"
[root@project1-master ~]# grep -f fileawithspacenexttothis -v fileb
that
these
thisthis
4.grep -f fileawithspacenexttothis -v -w fileb is equivalent to "grep -v -w 'this ' fileb" , it will display "thisthis" & "this this that" as there is no word boundary in them for "this "
[root@project1-master ~]# grep -f fileawithspacenexttothis -v -w fileb
that
these
thisthis
this this that
May be it will be usefull if you provide sample content of filea & fileb and grep output.
Answered By - confused genius