1

I have a file with a number of urls in the form:

http://www.edu-factory.org/wp/about/

I'd like to extract the sitehost of the url and append it to the end of the line:

http://www.edu-factory.org/wp/about/ www.edu-factory.org

Any idea how to do that?

1 Answer 1

2

One way to do that:

:s:\(.*//\)\([^/]*\)\(/.*\):\1\2\3 \2:

Explanation:

  • :s: - Using substitution
  • \(.*//\) - Capture the part up to the double slash (group 1)
  • \([^/]*\) - Capture everything that's not a slash (group 2)
  • \(/.*\) - Capture the first single slash and everything after it (group 3)
  • :\1\2\3 \2: - Output groups 1, 2 and 3 followed by a space and group 2 again

Edit:

You could sorten that to:

:s:\(.*//\)\([^/]*\)\(/.*\):& \2:

The ampersand outputs the whole matched string which includes the whole line since the regex begins and ends with .*. You could use \0 instead of the ampersand.

2
  • And is there an output group that contains the whole line in which there was a match?
    – sumek
    Feb 28, 2011 at 13:52
  • @sumek: Please see my edited answer. Feb 28, 2011 at 14:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .