Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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
share|improve this answer
    
Brilliant, both examples using sed & awk worked great! Cheers. –  SamCulley May 28 '13 at 7:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.