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.

3 comments:

ianemv said...

unsay sed bai?

aga said...

sed is a Unix/Linux utility that means for stream editor. You can even used it as a shell to interpret the language your using inside a shell script. sed is mostly used for parsing text files then doing some implementation on programming to transform data of your text file. Think like Perl or the famous Regular Expression. That's how it works.

Hussam said...

Great find, thanks. Never forget to use the -E parameter, otherwise it won't work!!