Grep -H not always returning file path when two matches are on the same line

grepregular expression

I am using grep to recursively look through files and pull out all strings matching a pattern. I want to print the file path: matched string.

-H should print the file path for each entry

-o should print just the matched string, omitting everything else on the line I don't want

-r recursive search.

I run:

grep -Hro [pattern] .

Most of the matches print with the filename, however a few just print the matched string. Are -H and -o mutually exclusive in that -o is trimming the filename?
Sample Output:

./path/foo.txt: foo
./path/bar.txt: foo
./path/baz.txt: foo
foo
./path/baz.txt: foo

The 4th foo match should also have a file associated with it, but it is not being printed.

I am getting behavior similar to this post, however I am using the -H flag, and the "/dev/null" solution does not work either.

$ grep --version
grep (BSD grep) 2.5.1-FreeBSD

EDIT:

After further exploring each match where the regex was not getting the file name, they all occur when two strings are matched on the same line.
For example this text file

foo
foo sometext foo
foo

Outputs

./path/foo.txt: foo
./path/foo.txt: foo
foo
./path/foo.txt: foo

Anyone know a solution to get both matches to print a filename?

Best Answer

No, -H and -o are not mutually exclusive. You may have a Carriage Return character in the part that is matched. This would make the following text be written at the start of the line, thus overwriting the file name.

$ printf 'foobar\n' | grep -Ho '.bar'
(standard input):obar

$ printf 'foo\rbar\n' | grep -Ho '.bar'
bar

Also, since both lines belong to the same file baz.txt (if your example is correct), this may also be due to a long line (larger than the screen width) being wrapped to the next line.

$ printf 'foo%80sbar\n' | grep -Ho 'foo.*'
(standard input):foo
       bar

Whether one of those scenarios may apply to your situation really depends on your search regex and the content of your files.

Related Question