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)

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!!