Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the page source of a website but the links that I need are clumped together. How do I separate each link, one link per line, if they all start with href?

share|improve this question

There are several ways to do it. For example, you can write a small program or script to do the work, or use a text editor capable of formatting HTML, or one that can do a search/replace using regular expressions. I'll show you the last two options, using jEdit (sudo apt-get install jedit), although there are plenty of other editors you can use.

Auto format the HTML text

You'll need one of the "Beauty" or "XML" plugins for jEdit. In jEdit, go to "Plugins" -> "Plugin Manager...", tab "Install", check "Beauty" or/and "XML", click button "Install". If you now go to the tab "Manage", the plugin(s) should be listed there.

To use Beauty, you'll need to make sure that the html formatter is selected. Go to "Plugins" -> "Plugin Options...". In the left tree, open "Plugins"/"Beauty"/"Modes". In the right list, select Beautifier html:beauty for Mode html.
jEdit - Beauty plugin configuration
Click OK. Now open your file and check that the edit mode is html (status bar, bottom left)
enter image description here
Run the plugin by going to "Plugins" -> [initials] -> "Beauty" -> "Beautify Buffer".

Or, to use XML, just go to "Plugins" -> [initials] -> "XML" -> "Indent XML".

Search/replace using regular expressions

Load your file in jEdit, press Ctrl+F to open the "Search And Replace" dialog and change the options so that it looks like this:

jEdit Search And Replace

That is: Search for: (<a(?:\s+[^>]*)?>.*?</a>). Replace with: \n$1\n. Be careful that "Regular expressions" is checked. Now press "Replace All" and all links should get a new line before and after. If you want the new line only before the link, replace with \n$1 instead; if you want it only after, replace with $1\n. Here's the reference of regular expressions in jEdit.

I tested the replace with:

<h1><a name="top"></a>Test</h1>
<div><a href="#top" title="link #1"><strong><em>first</em> link</strong></a><a href="page.html">page</a><!-- empty --><a href="l1.html"></a><abbr title="something">smt</abbr><p>more <a title="" href="#c3"> third </a> text</p></div>

Which resulted in:

<a name="top"></a>
<a href="#top" title="link #1"><strong><em>first</em> link</strong></a>

<a href="page.html">page</a>
<!-- empty -->
<a href="l1.html"></a>
<abbr title="something">smt</abbr><p>more 
<a title="" href="#c3"> third </a>
share|improve this answer
I'd like to be able to do it through sed or awk or some sort of built in editor. I don't have a lot of persistence either way so I'd rather not install other packages if I can use something built in. – user1883614 Dec 10 '12 at 21:41

Your Answer


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.