Question: I'm trying to use sed to clean up lines of URLs to extract just the domain.

So from:

```
http://www.suepearson.co.uk/product/174/71/3816/

```

I want:

```
http://www.suepearson.co.uk/

```

(either with or without the trailing slash, it doesn't matter)

I have tried:

```
 sed 's|\(http:\/\/.*?\/\).*|\1|'

```

and (escaping the non-greedy quantifier)

```
sed 's|\(http:\/\/.*\?\/\).*|\1|'

```

but I can not seem to get the non-greedy quantifier (`?`) to work, so it always ends up matching the whole string.

Answer: 
There is still hope to solve this using pure (GNU) sed. Despite this is not a generic solution in some cases you can use "loops" to eliminate all the unnecessary parts of the string like this:

```
sed -r -e ":loop" -e 's|(http://.+)/.*|\1|' -e "t loop"

```

* -r: Use extended regex (for + and unescaped parenthesis)
* ":loop": Define a new label named "loop"
* -e: add commands to sed
* "t loop": Jump back to label "loop" if there was a successful substitution

The only problem here is it will also cut the last separator character ('/'), but if you really need it you can still simply put it back after the "loop" finished, just append this additional command at the end of the previous command line:

```
-e "s,$,/,"

```
Here is something you can do with a two step approach and awk:

```
A=http://www.suepearson.co.uk/product/174/71/3816/  
echo $A|awk '  
{  
  var=gensub(///,"||",3,$0) ;  
  sub(/\|\|.*/,"",var);  
  print var  
}'  

```

> 
> Output:
>  <http://www.suepearson.co.uk>
> 
> 
> 

Hope that helps!