Generate CSV/TXT file from named zone file

1

I am in need of generating a csv/txt file from a zone file on a named DNS server.

For examples I would like to generate a list of the hostname and IP from the zone file

From this

monkey          A   192.168.1.1
bear            A   192.168.1.2
shark           A   192.168.2.1
bird            A   192.168.3.1
lion            A   192.168.4.1

To This

monkey,192.168.1.1
bear,192.168.1.2
shark,192.168.2.1
bird,192.168.3.1
lion,192.168.4.1

How can I achieve this, using Sed and Grep? Does anyone have a script that they use to do a similar task?

SamCulley

Posted 2013-05-21T12:20:40.870

Reputation: 63

Answers

1

Perhaps you would consider using awk for this:

awk '{print $1,$3}' OFS=, infile

If you are set on sed here is one way you can do it (GNU sed):

sed -r 's/([^ \t]+)\s+A\s+([^ \t]+)/\1,\2/' infile

Or with GNU coreutils:

<infile tr -s ' ' | cut --output-delimiter=, -d' ' -f1,3

Output:

monkey,192.168.1.1
bear,192.168.1.2
shark,192.168.2.1
bird,192.168.3.1
lion,192.168.4.1

Thor

Posted 2013-05-21T12:20:40.870

Reputation: 5 178

Brilliant, both examples using sed & awk worked great! Cheers. – SamCulley – 2013-05-28T07:45:14.497