How to search for text in a file ignoring newlines

grepnewlinessearchtext processing

I'd like to search for text that may be split over several lines in a file. A grep that would ignore line breaks and return the matching span of lines.

e.g. I would be searching for is an example file, and expect it to be found in the following file:

This is
an
example file.

Not to depend on leading or trailing spaces, entirely ignoring all forms of white space might be best (ideally, treating any sequence of white space as a single space).


One non-ideal solution is tr '\n' ' ' | grep, that discriminates between matches and non-matches, but doesn't show the match, nor deals well with big files.

Best Answer

The GNU grep can do it

grep -z 'is\san\sexample\sfile.' file

To fulfill some points which arise in comments there are some modifications to script:

 grep -oz '^[^\n]*\bis\s*an\s*example\s*file\.[^\n]*' file

Regarding huge files I have no imagination of memory limitation but in the case of problem you are free to use sed

sed '/\bis\b/{
          :1
          N
          /file\.\|\(\n.*\)\{3\}/!b1
         }
     /\<is\s*an\s*example\s*file\./p
     D' file

that keep no more than 4-lines (because 4 words in pattern) in memory (\(\n.*\)\{3\}).

Related Question