I have a file with a lot of lines.Some of the lines end in unmatched braces, i.e. }This is only true for lines that contain .toHow can I remove the unmatched braces with sed for ALL lines that contain a string (.to) but don't have another string ({) anywhere in the line (before or after the .to) but do end in }?Something like sed -i '/\.to/s/[[:space:]]*\}//' filebut that would be for ALL lines that contain a string (.to) and end in a } I want it for lines that don't have a brace/bracket, e.g. { before the }I do not need to worry about multi-line braces for this.  Limiting to lines that match a search ('.to' in this case) helps prevent that.e.g.  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 }
sed - remove unmatched }, ) or '] (only for the line in question)
sed;ruby;rspec
How about simplysed -i 's@^\([^{]\+\)\(\.to[^{]\+\)}\s*$@\1\2@' your_fileWith Perl (for better readability):perl -pi -e 's@                 ^                  # 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              @$1$2@x' your_file