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: 
Since PCRE is also tagged here, we could use GNU `grep` by using non-lazy match in regex `.*?` which will match first nearest match opposite of `.*`(which is really greedy and goes till last occurrence of match).

```
grep -oP '^http[s]?:\/\/.*?/' Input_file

```

***Explanation:*** using `grep`'s `oP` options here where `-P` is responsible for enabling PCRE regex here. In main program of `grep` mentioning regex which is matching starting http/https followed by `://` till next occurrence of `/` since we have used `.*?` it will look for first `/` after (http/https://). It will print matched part only in line.
```
echo "/home/one/two/three/myfile.txt" | sed 's|\(.*\)/.*|\1|'

```

don bother, i got it on another forum :)