简体   繁体   中英

Extracting lines from zone file

I have a DNS zone file that looks like the following:

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

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):

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

This terminates following the first occurrence of record , or:

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

This prints all occurrences of record .

You could also think about using a dns zone file parsers available in various languages like python etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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