PHP equivalent preg_replace () wrapper

Greetz people.

I am looking for a way to do the same thing as PHP preg_replace () (finding the text matching the regular expression and replacing it) in the shell script.

So, consider the following file.

<a href="http://example.com/">Website #1</a> <a href="http://example.net/">Website #2</a> <a href="http://example.org/">Website #3</a> 

And I want to get the following:

 http://example.com/ http://example.net/ http://example.org/ 

Is there any way to do this? Thanks.

+6
unix bash php regex preg-replace
source share
2 answers

You can use sed as:

 sed -r 's/.*href="([^"]*)".*/\1/' file 

Take a look

+9
source share

While sed great, it does not allow more than 9 backlinks. Perl does:

 echo "abcdefghijklmnopqrstu vwxyz" | \ perl -lpe 's/(\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+)/$1;$2;$3;$4;$5;$6;$7;$8;$9;$10;$11;$12;$13;$14;$15;$16;$17;$18;$19;$20;$21;$22;$23;$24;$25;$26/g' a;b;c;d;e;f;g;h;i;j;k;l;m;n;o;p;q;r;s;t;u;v;w;x;y;z 

This (dumb) example shows that you can go further sed \9

0
source share

All Articles