A Perl approach:
$ perl -F"" -a00ne 'for (@F){$i++ if /{/; $i||print; $i-- if /}/}' file
This is
that wants
anyway
Explanation
-a
: turns on automatic splitting on the file delimiter given by -F
into the @F
array.
-F""
: sets the input field separator to empty which will result in each element of @F
being one of the input characters.
-00
: turn on "paragraph mode", where a "line" is defined as two consecutive newline characters. This means that the entire file in this case will be treated as a single line. If your file can have many paragraphs and the brackets can span multiple paragraphs, use -0777
instead.
-ne
: read an input file and apply the script given by -e
to each line.
The script itself is actually quite simple. A counter is incremented by one every time a {
is seen and decremented by one for every }
. This means that when the counter is 0, we are not inside brackets and should print:
for (@F){}
: do this for each element of @F
, each character in the line.
$i++ if /{/;
: increment $i
by one if this character is a {
$i||print;
: print unless $i
is set (0 counts as unset).
$i-- if /}/
: decrement $i
by one if this character is a }