Shell equivalent to PHP's preg_replace()

seriousdev picture seriousdev · Dec 20, 2010 · Viewed 7.1k times · Source

Greetz folks.

I'm looking for a way to do the same stuff than PHP's preg_replace() does (search text matching a regular expression and replace it) in a 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 this:

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

Is there a way to do this? Thanks.

Answer

codaddict picture codaddict · Dec 20, 2010

You can use sed as:

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

See it