0

I want to print out the same lines in a.txt and b.txt. I think "grep -xf a.txt b.txt" meets my needs. But it doesn't work properly. My system environment is MacOs Mojave

[yangyue ~/tempDir]$ cat a.txt
123 abc
123 abc jjj
123
456
zzz
[yangyue ~/tempDir]$ cat b.txt
123 abc
123 abc jjj
123
456def
456
xyz
[yangyue ~/tempDir]$ grep --color=never -xf a.txt b.txt
123 abc
123
456
[yangyue ~/tempDir]$

the expected output in this case

123 abc
123 abc jjj
123
456

The encoding of these two files is the same. There is no space at the end of each line. I thinks the reason is "123 abc" is the prefix of "123 abc jjj" Then I did two tests.

test1

[yangyue ~/tempDir]$ cat 1.txt
a
ab
abc
[yangyue ~/tempDir]$ cat 2.txt
a
ab
abc
[yangyue ~/tempDir]$ grep --color=never -xf 1.txt 2.txt
a
[yangyue ~/tempDir]$

test2

[yangyue ~/tempDir]$ cat 3.txt
abc
ab
a
[yangyue ~/tempDir]$ cat 4.txt
abc
ab
a
[yangyue ~/tempDir]$ grep --color=never -xf 3.txt 4.txt
abc
ab
a
[yangyue ~/tempDir]$

Is this grep bug or is my usage wrong?

yue.yang
  • 19
  • 3
  • Your usage is wrong. `grep -x` requires the whole line to match. If you have a prefix that matches, but then other content that doesn't, having that other content means the whole line does not actually match. – Charles Duffy Dec 28 '18 at 04:07
  • ...btw, it would be a stronger question if it explained *why* you think your stated expected output to be correct. – Charles Duffy Dec 28 '18 at 04:08
  • thank, I know `grep -x` requires the whole line to match. but the line `123 abc jjj` both in a.txt and b.txt, it should match in the b.txt, but is doesn't output – yue.yang Dec 28 '18 at 04:14
  • I think `grep -xf a.txt b.txt` should works like `egrep -x "123 abc|123 abc jjj|123|456|zzz" b.txt`. And the output of this command `egrep -x "123 abc|123 abc jjj|123|456|zzz" b.txt` meets my expectations. – yue.yang Dec 28 '18 at 04:21
  • I'm geting the expected result on Linux. Are you sure you don't have some trailing space lurking there? – James Brown Dec 28 '18 at 06:45
  • @JamesBrown I find a linux server and did my test, I got the expected output. `grep -xf` may have bugs on MacOs. I use `grep --color=never` in this test case, because it's also a bug in MAC [grep -f on OS X produces segfault](https://stackoverflow.com/questions/17247142/grep-f-on-os-x-produces-segfault) – yue.yang Dec 28 '18 at 08:36
  • I'm still unconvinced. A reproducer that creates the input files would give us assurance that they really have the values you think they do (and don't have invisible differences, say, CRLFs rather than LF-only traditional UNIX newlines). – Charles Duffy Dec 28 '18 at 16:38

1 Answers1

0

Thanks JamesBrown

I find a Linux server and did my test, I got the expected output. grep -xf may have bugs on MacOs. I use grep --color=never in this test case, because it's also a bug on MacOs grep -f on OS X produces segfault

In this case, I got the expected output on MacOs by doing this. But I don't know if this is a general solution.

[yangyue ~/tempDir]$ sort -r a.txt > c.txt
[yangyue ~/tempDir]$ sort -r b.txt > d.txt
[yangyue ~/tempDir]$ cat c.txt
zzz
456
123 abc jjj
123 abc
123
[yangyue ~/tempDir]$ cat d.txt
xyz
456def
456
123 abc jjj
123 abc
123
[yangyue ~/tempDir]$ grep --color=never -xf c.txt d.txt
456
123 abc jjj
123 abc
123
[yangyue ~/tempDir]$
yue.yang
  • 19
  • 3
  • I used this method to manipulate a large amount of data and found that there were still errors on MacOs. Finally, I wrote a python script to solve the problem. – yue.yang Dec 28 '18 at 11:16