# sed - remove unmatched "}", ")" or ']" (only for the line in question)

I have a documents with a great deal of lines.

Several of the lines end in unparalleled dental braces, i.e. "}".

This is just real for lines which contain ".to"

How can I remove the unparalleled dental braces with sed for ALL lines which contain a string (".to") yet do not have an additional string ("{") throughout the line (prior to or after the .to) yet do end in "}"?

Something like sed -i '/\.to/s/[[:space:]]*\}//' file

yet that would certainly be for ALL lines which contain a string (".to") and also end in a "}"

I desire it for lines that do not have a brace/bracket, as an example { prior to the }

I do not require to bother with multi - line supports for this. Restricting to lines that match a search ('. to' in this instance) aids protect against that.

as an example

  expect(:size).to eq 2 }
expect(:first).to be_persisted }
expect("first.number_field").to eq 10 }
expect(:last).to be_persisted }
expect("last.number_field").to eq 10 }
this remains unchanged }
this {remains} unchanged }
this {
is ok }

=>

expect(:size).to eq 2
expect(:first).to be_persisted
expect("first.number_field").to eq 10
expect(:last).to be_persisted
expect("last.number_field").to eq 10
this remains unchanged }
this {remains} unchanged }
this {
is ok }

2
2022-07-25 20:39:10
Source Share

How around merely

sed -i '[email protected]^$[^{]\+$$\.to[^{]\+$}\s*[email protected]\1\[email protected]' your_file


With Perl (for far better readability) :

perl -pi -e '[email protected]
^                  # start of line
( [^{]+ )        # at least one non-{ character (save in $1) (\.to [^{]+ ) # .to and at least one non-{ (save in$2)
}                # that pesky }
\s*              # optional white space
\$                  # end of line
@[email protected]' your_file

2
2022-07-25 21:21:21
Source