I\'m looking for a quick and easy one-liner to extract all brace-delimited text-blocks containing a search string from a text file. I\'ve just about googled myself crazy on
User 999999999999999999999999999999 had a nice answer using sed
which I really liked, unfortunately their answer appears to have disappeared for some reason.
Here it is for those who might be interested:
sed '/{/{:1; /}/!{N; b1}; /event/p}; d' filepath
Explanation:
/{/ if current line contains
{then execute next block
{ start block
:1; label for code to jump to
/}/! if the line does not contain
}then execute next block
{ start block
N; add next line to pattern space
b1 jump to label 1
}; end block
/event/p if the pattern space contains the search string, print it
(at this point the pattern space contains a full block of lines
from
{to
})
}; end block
d delete pattern space