Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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.

share|improve this answer
    
And is there an output group that contains the whole line in which there was a match? –  sumek Feb 28 '11 at 13:52
    
@sumek: Please see my edited answer. –  Dennis Williamson Feb 28 '11 at 14:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.