文件

文件

我的文件中有以下内容。我想使用 Vim 或终端来提取标题及其内容,如下所示。

我怎样才能实现这个目标?

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.
....
....

文件

"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

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

解释

  • grep -oP:使用grep,仅打印-o使用Perl语法的匹配项-p
  • (?<="title":"): Perl 向后查找。匹配其前面的字符串"title":"
  • .*?:要匹配(和打印)的实际部分。在前瞻(下一个)之前匹配“非贪婪”数量的字符。
  • (?="): Perl 前瞻。匹配"其后的字符串。

答案2

既然这是JSON,我会使用 JSON 解析器。

Python 有一个使用起来非常简单的 JSON 库 — 只需进行一个函数调用,您就可以获取 Python 数据结构形式的数据,然后可以对其进行迭代。

#!/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)

相关内容