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: 
[sed - non greedy matching by Christoph Sieghart](http://0x2a.at/blog/2008/07/sed--non-greedy-matching/)

The trick to get non greedy matching in sed is to match all characters excluding the one that terminates the match. I know, a no-brainer, but I wasted precious minutes on it and shell scripts should be, after all, quick and easy. So in case somebody else might need it:

Greedy matching

```
% echo "<b>foo</b>bar" | sed 's/<.*>//g'
bar

```

Non greedy matching

```
% echo "<b>foo</b>bar" | sed 's/<[^>]*>//g'
foobar

```
If you have access to gnu grep, then can utilize perl regex:

```
grep -Po '^https?://([^/]+)(?=)' <<< 'http://www.suepearson.co.uk/product/174/71/3816/'
http://www.suepearson.co.uk

```

Alternatively, to get everything *after* the domain use

```
grep -Po '^https?://([^/]+)\K.*' <<< 'http://www.suepearson.co.uk/product/174/71/3816/'
/product/174/71/3816/

```