Tuesday, February 9, 2010

Using sed with backreference as the replacement pattern

Tinkering with sed, I have manage to try using regular expressions using backreferences using the most common \N where N is the number of position inside the parenthesis.

Searches and replaces the zoo.color to zoo['color'] or zoo.animals to zoo['animals']:
cat zoo.js | sed -E "s/(zoo)(\.)([a-zA-Z0-9]*)/\1['\3']/g"|less

\1 = is the characters inside the first parenthesis w/c is the zoo
\3 = is the characters inside the third parenthesis which involves alpha-numeric

-E will interpret as regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's)

Another example, you can use this for replacing such a string let say, in a SQL statement,

example SQL string:

CREATE DEFINER=`mysqluser`@`%` FUNCTION `total`(cost DOUBLE, income DOUBLE, lng2 DOUBLE) RETURNS double

And you wanted to remove DEFINER=`mysqluser`@`%` and allow FUNCTION to be a dynamic string so you'll just need to catch that string. So you can use backreferences with sed.

$> cat query.sql | sed -e 's/DEFINER.*\@.*\s\([A-Za-z]*\)\s`/\'1' `/g'

result:

CREATE FUNCTION `total`(cost DOUBLE, income DOUBLE, lng2 DOUBLE) RETURNS double


Hope this helps.