![如何提取一行及其后的一系列行](https://linux22.com/image/114755/%E5%A6%82%E4%BD%95%E6%8F%90%E5%8F%96%E4%B8%80%E8%A1%8C%E5%8F%8A%E5%85%B6%E5%90%8E%E7%9A%84%E4%B8%80%E7%B3%BB%E5%88%97%E8%A1%8C.png)
domain:
我正在尝试提取包含后跟从nameservers"
to 的行范围的单行]
。我知道如何分别使用grep
和awk
分别完成这些事情,但不知道如何立即完成。
输入数据:
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>