0

I would like to grep for a literal string which contains newlines. I found this question but the accepted answer suggests the -F option, which uses a set of newline-separated strings. I also found this question which uses pcregrep with the -M option, but that matches regexes and not just literal strings.

So how can I test if a file contains a specific, multiline, literal string?

Community
  • 1
  • 1
Lars Nyström
  • 5,786
  • 3
  • 32
  • 33
  • Do you want to search for a specific multi-line string? Why are regexes not applicable? You may also just escape all special characters, and then use `pcregrep -M`. – Martin Nyolt Aug 23 '16 at 14:49
  • Yes, a specific string. Edited the question. I don't want to use regexes because the string to search for contains a bunch of special characters I would then have to escape. It's also a sort of big file (~150MB) so my guess was that a literal search would be faster. – Lars Nyström Aug 23 '16 at 14:50
  • Checking simple regular expressions is only negligible slower than fixed string checking (in both cases, the string has to be scanned only once). If optimising to the last millisecond is not important, you can go with escaping the string. And 150MB is not that big of a deal if you don't have to do it every second. – Martin Nyolt Aug 23 '16 at 15:00

2 Answers2

0

Here's one way you could do it using awk:

awk -v str="$string" 'BEGIN { n = split(str, lines, /\n/); i = 1 } 
    lines[i] == $0 { if (i++ == n) print "matched"; next } { i = 1 }' file

Split the string into an array of lines. n is the total number of lines (returned by split) and i keeps track of the current line to compare.

If the current line matches and we have reached the last line, print the message and skip to the next line. Otherwise, reset the line counter back to 1.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
-1

Just check for carriage return character and new line, if the line index grows, but there is no carriage return or new line char (depending on OS) there IS a multiline string. Im not sure if this can be done with grep but should be quite easy to implement with any programming language or even bash

  • Ah, sorry I was a bit unclear. What I meant was that I want to search for a specific multiline string, not just any multiline string. I'll edit the question. – Lars Nyström Aug 23 '16 at 14:48