1

I have the following in a file. I'd like to use Vim or terminal to extract title and its content like this.

How can I achieve this?

Topic 1 - Core: Algebra
1.1
Arithmetic sequences and series; sum of finite arithmetic series; geometric sequences and series; sum of finite and infinite geometric series.
Sigma notation.
Applications.
1.2
Exponents and logarithms.
....
....

file

"json_data" : {"data":[[{"data":{"title":"Topic 1 - Core: Algebra","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1885,"id":"syllabus_section_tree_node_1885"},"children":[{"data":{"title":"1.1","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1886,"id":"syllabus_section_tree_node_1886"},"children":[{"data":{"title":"Arithmetic sequences and series; sum of finite arithmetic series; geometric sequences and series; sum of finite and infinite geometric series.","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1895,"id":"syllabus_section_tree_node_1895"}},{"data":{"title":"Sigma notation.","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1896,"id":"syllabus_section_tree_node_1896"}},{"data":{"title":"Applications.","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1897,"id":"syllabus_section_tree_node_1897"}}]},{"data":{"title":"1.2","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1887,"id":"syllabus_section_tree_node_1887"},"children":[{"data":{"title":"Exponents and logarithms.","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1898,"id":"syllabus_section_tree_node_1898"}},{"data":{"title":"Laws of exponents; laws of logarithms.","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1899,"id":"syllabus_section_tree_node_1899"}},{"data":{"title":"Change of base.","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1900,"id":"syllabus_section_tree_node_1900"}}]},{"data":{"title":"1.3","attr":{"href":"javascript:void(0);"}},"attr":{"data-record-id":1888,"id":"syllabus_section_tree_node_1888"},

... ...

1
2
grep -oP '(?<="title":").*?(?=")' <INPUT_FILE>

Explanation

  • grep -oP: Use grep, printing only matches -o using Perl syntax -p.
  • (?<="title":"): Perl lookbehind. Match a string that has "title":" before it.
  • .*?: The actual part to match (and print). Match a "non-greedy" amount of characters before the lookahead (next).
  • (?="): Perl lookahead. Match a string that has " after it.
0

Since this is JSON, I'd use a JSON parser.

Python has a JSON library that's very simple to use — just make one function call and you get the data as a Python data structure which you can then iterate over.

#!/usr/bin/python
import json, sys
def explore(tree):
    if isinstance(tree, list):
        for child in tree:
            explore(child)
        return
    if tree.has_key("title"):
        print tree["title"]
    if tree.has_key("data"):
        explore(tree["data"])
    if tree.has_key("children"):
        explore(tree["children"])
data = json.loads("{" + sys.stdin.read() + "}")["json_data"]
explore(data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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