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?