3 added 4 characters in body
source | link
$ 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

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.

$ 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
$ 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.

2 added 4 characters in body
source | link
$ sed ':again;N;$again;$!N;$!b again; s/{[^}]*}//g' file
This is 
that wants
 anyway.

Explanation:

  • :again;N;$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
$ 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
$ 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
1
source | link

$ 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