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

  • by the way when I test the same thing in the regexr.com it works fine! (you need to remove escapes and quotation there to work) – Farhad Nov 1 '16 at 23:23
  • @KasiyA So I'm trying the first given command in the given solution from: askubuntu.com/questions/551338/… and I get the following error: grep: unescaped ^ or $ not supported with -Pz – Farhad Nov 2 '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 2 '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 3 '16 at 1: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 3 '16 at 1:53
3

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?
}
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.