简体   繁体   English

从区域文件中提取行

[英]Extracting lines from zone file

I have a DNS zone file that looks like the following: 我有一个DNS区域文件,如下所示:

record A 1.1.1.1
       A 1.1.1.2
       A 1.1.1.3
other_record A 1.1.1.4
             A 1.1.1.5
another_rec  A 1.1.1.6

I need to extract all lines that belong to record (so the first one and the two below it) without any of the other lines (the actual file has many more records like this so greping the first line and the 2 other below is not a valid solution). 我需要提取属于记录的所有行(因此它下面的第一行和第二行)而没有其他任何行(实际文件中有更多这样的记录,因此,请抓住第一行和下面的其他2行不是有效的解决方案)。

Expected result: 预期结果:

record A 1.1.1.1
       A 1.1.1.2
       A 1.1.1.3
awk '/^[^[:space:]]/{f = ($1=="record" ? 1 : 0)} f' file

This might work for you (GNU sed): 这可能对您有用(GNU sed):

sed '/^record/{:a;n;/^\S/Q;ba};d' file

This terminates following the first occurrence of record , or: 这在第一次出现record或以下record终止:

sed '/^\S/h;G;/^record/MP;d' file

This prints all occurrences of record . 打印所有出现的record

You could also think about using a dns zone file parsers available in various languages like python etc. 您还可以考虑使用dns区域文件解析器,这些解析器可使用python等各种语言提供。

Once you have a handle on zone file content as an object then its pretty easy to manipulate it the way you want. 一旦将区域文件内容作为对象进行处理,则可以很容易地按所需方式对其进行操作。

I got this one after doing quick web search - https://github.com/blockstack/zone-file-py . 经过快速的网络搜索-https://github.com/blockstack/zone-file-py我得到了这个。 (there are bunch of others... you need to see which one works for you best) (还有很多其他...您需要查看哪个最适合您)

您能否尝试遵循并让我知道是否有帮助。

awk '/^[a-z]+/ && !/^record/{flag=""} /^record/{flag=1} flag'  Input_file
awk '!/_/&&NR!=5' file

record A 1.1.1.1
       A 1.1.1.2
       A 1.1.1.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM