What you need is a grep
that can recognise patterns across line endings ("hello" followed by anything (possibly even line endings), followed by "hello")
As grep
processes your files line by line, it is (by itself) not the right tool for the job - unless you manage to cram the whole file into one single line.
Now, that is easy, for example using the tr
command, replacing line endings by spaces:
if cat $file | tr '\n' ' ' | grep -q 'hello.*hello'
then
echo "$file matches"
fi
This is quite efficient, even on large files with many (say 100000) lines, and can be made even more efficient by calling grep
with --max-count=1
, making it stop the search after a match has been found. It doesn't matter whether the two hellos are on the same line or not.