I’d like to change
{foo, {bar}, foobar}
to
{foo, bar, foobar}
in all rows that match '{.*{'. I.e. remove all curly braces { and } except the outer most pair.
So doing
mysql -h $H -u $U -p$P $DB -B -e "SELECT id FROM t WHERE col REGEXP '{.*{'" > bad.txt
selects all the rows that will need this substitution. How do I make this substitution very quickly?
EDIT:
Could I do it by
update table set column = REPLACE(column,'{','');
Then restore the out most pair
update table set column = REPLACE(column,'^','{');
update table set column = REPLACE(column,'$','}');
I get error
mysql> EXPLAIN UPDATE t SET col=REPLACE(REPLACE(REPLACE(REPLACE(col,'{{','{'),'}}','}'), ', {', ', '), '}, ', ', ');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE t SET col=REPLACE(REPLACE(REPLACE(REPLACE(' at line 1
Unfortunately MySQL doesn’t support back references in regular expressions(allow grouping subexpressions with parentheses and recalling the value they match in the same expression).
you can export the result of your SELECT query into a text file (e.g. in CSV format), replace all occurrence of
with:
with a text editor which supports the Perl regular expression syntax, and then replace the original rows with modified ones.
notice that this query:
will work only if the entire text is guaranteed to be wrapped by a pair of braces!