如何提取一行及其后的一系列行

如何提取一行及其后的一系列行

domain:我正在尝试提取包含后跟从nameservers"to 的行范围的单行]。我知道如何分别使用grepawk分别完成这些事情,但不知道如何立即完成。

输入数据:

domain: stackexchange.com date: Mon Jul 3 00:43:49 2017 output_dir:        /tmp/stackexchange.com.12653
INFO:root:crawl: exiting dom: 'stackexchange.com' took: 10s
INFO:root:2017-07-03 00:44:06:370 slave.py: exiting args.url: 'stackexchange.com' took: 3s
+ comparing web systems

  "mail_server_ip": [],              |   "mail_server_ip": []
  "nameservers": [
    "ns-925.awsdns-51.net.",
    "ns-1029.awsdns-00.org.",
    "ns-cloud-d1.googledomains.com.",
    "ns-cloud-d2.googledomains.com.",
  ],
  "nameservers_domains": [           |   "nameservers_domains": [], 
    "m

期望的输出:

  domain: stackexchange.com date: Mon Jul 3 00:43:49 2017 output_dir:        
  "nameservers": [
    "ns-925.awsdns-51.net.",
    "ns-1029.awsdns-00.org.",
    "ns-cloud-d1.googledomains.com.",
    "ns-cloud-d2.googledomains.com.",
  ],

单独提取数据的命令:

  grep "domain:" test_sample.txt      
  awk '/nameservers"/,/]/' test_sample.txt

答案1

awk方法

awk '/^domain:/{print}/"nameservers":/,/]/' test_sample.txt

答案2

grep接近P聚合酶链式反应) 选项:

grep -Poz 'domain: .+ output_dir:|\s*"nameservers": \[[^][]+\],\n' test_sample.txt

输出:

domain: stackexchange.com date: Mon Jul 3 00:43:49 2017 output_dir:
  "nameservers": [
    "ns-925.awsdns-51.net.",
    "ns-1029.awsdns-00.org.",
    "ns-cloud-d1.googledomains.com.",
    "ns-cloud-d2.googledomains.com.",
  ],

主要模式基于正则表达式交替组<domain_line>|<nameservers_lines>

相关内容