Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .