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?
1 Answer
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.
Click OK. Now open your file and check that the edit mode is html (status bar, bottom left)
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:
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:
<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>
-
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. Dec 10, 2012 at 21:41