1

I have something like this in a file testtt:

{It captures this! }
// question: 2572410  name: Question 2

::Question 2::[html] Is it going to be -40 tomorrow?

{
It can't
capture this!!! why?
}

when I do:

grep -o '{\([^}]*\)}' testttt

It can't capture the multi-line braces. Any help to modify it in way that it could capture both would be apppreciated!

PS. I have also tested the given solution from: How do I grep for multiple patterns on multiple lines? and it gives the following error:

grep: unescaped ^ or $ not supported with -Pz

You can find the text file of the output and file contents here

Farhad
  • 129
  • by the way when I test the same thing in the http://regexr.com/ it works fine! (you need to remove escapes and quotation there to work) – Farhad Nov 01 '16 at 23:23
  • @KasiyA So I'm trying the first given command in the given solution from: http://askubuntu.com/questions/551338/how-do-i-grep-for-multiple-patterns-on-multiple-lines and I get the following error: grep: unescaped ^ or $ not supported with -Pz – Farhad Nov 02 '16 at 18:43
  • Could you please post text files, dialogue messages, and program output listings as text, not as images? To achieve the latter two you can either 1) select, copy & paste the dialogue text or terminal content or 2) save the program output to a file and use that. Longer listings (>100 lines) should be uploaded to a pastie service and linked to in the question. Thanks. – David Foerster Nov 02 '16 at 20:42
  • Your original question was about a multiline match between braces, if you have another question about matching line beginnings / endings when in -z (null terminated) mode you should ask that separately IMHO (the answer will probably be to use explicit \n characters, something like grep -Pzo "begin\n(.|\n)*\nend" or grep -Pzo "(?s)begin\n.*\nend") – steeldriver Nov 03 '16 at 01:21
  • @steeldriver The reason that I edited my question was it go marked duplicated by different users so I tried the given solution in the refered post to test that is working for me or not!, removing caret and $ sign will make it work in this case but still this doesn't change the fact that the given solution maybe needs to be edited! Thanks for your attention! – Farhad Nov 03 '16 at 01:53

2 Answers2

4

By default, grep reads and processes single lines.

In newer versions of grep, you can use the -z option to tell it to consider its input to be null separated instead of newline separated; since your input doesn't have null terminations, that's essentially equivalent to perl's 'slurp' mode. So you could do

$ grep -zPo '{[^}]*}' testttt
{It captures this! }
{
It can't
capture this!!! why?
}

or, more perlishly, using a .*? non-greedy match with (?s) to include newlines in .

$ grep -zPo '(?s){.*?}' testttt
{It captures this! }
{
It can't
capture this!!! why?
}

Alternatively, if pcregrep is available,

$ pcregrep -Mo '(?s){.*?}' testttt
{It captures this! }
{
It can't
capture this!!! why?
}
steeldriver
  • 136,215
  • 21
  • 243
  • 336
2

In order to trigger multi-line search with grep you have to add few option more, so try:

 grep -Pzo '(?s){.*?}' testttt

Solution with nice explanation can be found (and is stolen:) ) from stackoverflow.

If you have pcregrep you may find it more useful in general case as it supports PERL 5 regex.