10

Example:

This is {
the multiline
text file }
that wants
{ to be
changed
} anyway.

Should become:

This is 
that wants
 anyway.

I have found some similar threads in the forum, but they don't seem to work with multi-line curly brackets.

If possible, I would prefer some one-line method, like solutions based on grep, sed, awk... etc.

EDIT: Solutions seem to be OK, but I have noticed that my original files include curly brackets nesting. So I am opening a new question. Thanks you everybody: How can I delete all text between nested curly brackets in a multiline text file?

  • 1
    Try this sed '/{/{:1;N;s/{.*}//;T1}' multiline.file – Costas Nov 8 '14 at 23:06
10
$ sed ':again;$!N;$!b again; s/{[^}]*}//g' file
This is 
that wants
 anyway.

Explanation:

  • :again;$!N;$!b again;

    This reads the whole file into the pattern space.

    :again is a label. N reads in the next line. $!b again branches back to the again label on the condition that this is not the last line.

  • s/{[^}]*}//g

    This removes all expressions in braces.

On Mac OSX, try:

sed -e ':again' -e N -e '$!b again' -e 's/{[^}]*}//g' file

Nested Braces

Let's take this as a test file with lots of nested braces:

a{b{c}d}e
1{2
}3{
}
5

Here is a modification to handle nested braces:

$ sed ':again;$!N;$!b again; :b; s/{[^{}]*}//g; t b' file2
ae
13
5

Explanation:

  • :again;$!N;$!b again

    This is the same as before: it reads in the whole file.

  • :b

    This defines a label b.

  • s/{[^{}]*}//g

    This removes text in braces as long as the text contains no inner braces.

  • t b

    If the above substitute command resulted in a change, jump back to label b. In this way, the substitute command is repeated until all brace-groups are removed.

  • Your answer seems to be perfect. As long as the new question I have just opened (read original question EDIT) is not exactly the same, I think you should answer it too. Would it be OK with forum rules? – Sopalajo de Arrierez Nov 8 '14 at 23:38
  • @John1024, you could move your edit over here as the OP posted a new question regarding the same. – Ramesh Nov 8 '14 at 23:38
  • 1
    OK. I have copied it over there and modified it to use the sample text in the new question. – John1024 Nov 8 '14 at 23:42
4

Perl:

perl -0777 -pe 's/{.*?}//sg' file

If you want to edit in-place

perl -0777 -i -pe 's/{.*?}//sg' file

That reads the file as a single string and does a global search-and-replace.

This will handle nested braced:

perl -ne 'do {$b++ if $_ eq "{"; print if $b==0; $b-- if $_ eq "}"} for split //'
4

Sed:

sed '/{/{:1;N;s/{.*}//;T1}' multiline.file

started since line with { and get the next line (N) until substitution ({}) can be made ( T means return to mark made by : if substitution isn't made)

A little bit modify to be true if many curle bracked in one line

sed ':1; s/{[^}]*}// ; /{/ { /}/!N ; b1 }' multiline.file

Remove all symbols in the brackets ([^}] equal every symbol exept right bracket to make sed not greedy), and if in the line remain left bracked - back to start with next line added if there isn't right bracket.

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.