Reputation: 891
I'm using bash to dynamically find and replace a string with another string e.g.
href="/controller/action"
href="<?php echo $this->url("controller", action-> "action") ?>"
I'm using this sed command to find and change it which works fine...
sed -i 's%href=\"/controller/action%href=\"<?php echo $this->url(\"controller\", action-> \"action\") ?>%g' changes.txt
What I want is for the old string to look for any type following a pattern using a wildcard. I saw that this is an option but doesn't work.
sed -i 's%href=\"/(.*)/(.*)%href=\"<?php echo $this->url(\"/1\", action-> \"/2\") ?>%g' changes.txt
How will I be able to run this command so that the controller and action can be anything and dynamically taken from the old string and inputted into the new string?
Upvotes: 1
Views: 94
Reputation: 626870
You are using a greedy dot matching pattern the is almost never what you need since a dot matches any symbol and *
quantifier grabs as many chars as it can. What you need is a negated bracket expression [^/]*
matching zero or more chars other than /
.
Also, since you are using a BRE POSIX regex you must escape the parentheses capturing a part of the pattern, else they denote literal (
and )
symbols.
Use
sed -i 's%href="/\([^/]*\)/\([^/]*\)%href="<?php echo $this->url("\1", action-> "\2") ?>%g' changes.txt
Upvotes: 2