Monday, 18 November 2013

Use sed to retreive links from a Firefox bookmark file

Sed is a great text manipulation tool. It allows to manipulate text through simple substitution commands.

It did occur to me that I had a few backup .json files in which Firefox bookmarks were stored. As I did not want to overwrite the existing bookmarks, but I wanted to retreive just a few links, I tried to open the file with a text editor.

The result was not pleasant:



I decided to use some CLI magic to transform this in a list of URLs which is easily readable, instead. It can be done in one line:

$ sed "s/\"uri\"\:\"/\\`echo -e '\n\r'`/g" bookmarks.json | sed "s/\"[^@]*$//" > new_bookmarks.txt

Where:   bookmarks.json -> bookmark file name
            new_bookmarks.txt -> new file name where the links will be saved

Enjoy!