Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk formatting and splitting 1

Status
Not open for further replies.

cryptoadm

MIS
Nov 6, 2008
71
US
How can this be done in awk?

The output of the list of resolv.conf is like this:

hostA:domain abc.com
hostA:search abc.com xyz mno
hostA:nameserver 1.1.1.1
hostA:nameserver 2.2.2.2
hostB:domain abc.com
hostB:nameserver 1.1.1.1
hostB:nameserver 2.2.2.2
hostB:nameserver 3.3.3.3
hostB:nameserver 4.4.4.4
hostC:domain def.com
hostC:nameserver 7.7.7.7
hostC:nameserver 8.8.8.8
hostC:nameserver 1.9.2.1
hostC:search def.com x24.com


The output would be:

hostA hostB hostC
domain abc.com abc.com def.com
nameserver 1.1.1.1 1.1.1.1 7.7.7.7
nameserver 2.2.2.2 2.2.2.2 8.8.8.8
nameserver 3.3.3.3
nameserver 4.4.4.4 1.9.2.1
search abc.com def.com
search xyz x24.com
search mno

Thanks!!
 
I found this a difficult one to come up with an elegant solution for due to the output format... try this:

Code:
awk -F '[: ]' '
        function printfield(fieldname,a,n) {
                for (i=0; i<n; i++) {
                        printf "%-20s",fieldname
                        for (h in hosts) { printf "%-15s",a[h,i] }
                        printf "\n"
                }
        }
        BEGIN { maxnsindex=0; maxsindex=0 }
        !($1 in hosts) { hosts[$1]; nsindex=sindex=0 }
        /:domain/ { domain[$1,0]=$3 }
        /:nameserver/ {
                nameserver[$1,nsindex++]=$3
                if (nsindex>maxnsindex) maxnsindex=nsindex
        }
        /:search/ {
                for (i=3; i<=NF; i++) { search[$1,sindex++]=$i }
                if (sindex>maxsindex) maxsindex=sindex
        }
        END {
                printf "%-20s",""
                for (h in hosts) printf "%-15s",h
                printf "\n"

                printfield("domain",domain,1)
                printfield("nameserver",nameserver,maxnsindex)
                printfield("search",search,maxsindex)
        }
' inputfile

Annihilannic.
 
Spectacular!

I didn't notice in my data I can have multiple domains for each host just like nameservers and searches.

for example:
hostA hostB
domain abc.com abc.com
domain xyz.com
nameserver 1.1.1.1 1.1.1.1
nameserver 2.2.2.2
search abc.com abc.com
search xyz.com

There are two domains for hostA and this isn't typical but for some hosts there are some. Just need to add the other domain lines.

Thank you.
 
I don't think that's valid syntax for a resolv.conf? I presume it only uses the last domain entry it encounters.

Also, on most OS only 3 nameserver entries are used.

Anyway, you should be able to figure out how to modify the handling of the multiple domain entries by modifying the script to treat domain entries in a similar way to the nameserver entries. You'll need to add a maxdindex variable or similar to track the maximum number of domain entries found in the listed hosts.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top