tags:

views:

450

answers:

3

I work for a company that offers webhosting and DNS services. We are trying to migrate everything over to new servers and we've often run into stale zonefiles on our servers as customers have changed their authoritative servers to someone else.

I am trying to write a script that will check whois, ns, and SOA information to determine if a domain still uses us as an authoritative server.

I am attempting to use SED to filter the output of the following to display only the nameserver without the trailing ".":

dig +noall +answer soa yahoo.com | sed 's/.*SOA\s*//' | sed 's/\.$//'

I've searched the net, but most SED command examples show out to count "lines" instead of "Words".

The desired output would be as follows:

    $ dig +noall +answer soa yahoo.com | sed '<Some SED code here>'
      ns1.yahoo.com

Any suggestions would be helpful.

+2  A: 

To count the number of words on a line:

awk '{ print NF }'

For instance:

printf 'foo bar\nbar\nfoo bar zim' | awk '{ print NF }'

Prints:

2
1
3
Ollie Saunders
+3  A: 

Rather than using removing the part that you don't want, I would suggest using a bracketed subexpression to identify the bit that you do want, as follows:

dig +noall +answer soa yahoo.com | sed 's/.*SOA\s*//' | sed 's/^\([^ ]*\). .*/\1/g'

This searches for a string beginning with some non-space characters, followed by a dot, a space, and then the rest of the line. It replaces that pattern with just the bit before the dot (which is subexpression #1).

David
Wow, I've been racking my brain over this one for a few hours. Thanks for the tip. Any recommended references? Just getting started with SED and haven't found alot of good documentation thus far.
To tell you the truth, I've only touched the tip of the `sed` iceberg myself - just the `s` command for search-and-replace - but I use it all the time. The command I gave you shows a common pattern that I've come to use quite a lot: matching characters up to a certain delimiter. This can be done by using the `^` character to exclude the delimiter from a range, e.g. `[^ ]*` when the delimiter is a space. Another useful feature is subexpressions, and they're a bit painful in `sed` because the brackets need to be escaped, hence the `\(...\)` above.
David
Oh, and here's a reference I found, which looks like it has some good stuff in it: http://www.grymoire.com/Unix/Sed.html
David
Why do the brackets need to be escaped?
Ollie Saunders
@Ollie: by default, sed uses basic regular expressions, in which parentheses (and many other useful RE characters) don't have any special meaning unless escaped. You may be more used to "extended" regular expressions, where escaping these characters turns OFF their special functions. Read `man re_format` for more details.Some versions of sed can use extended REs if you specify the `-E` flag.
Gordon Davisson
+3  A: 

when working with structured data, its easier to use awk

$ dig +noall +answer soa yahoo.com | awk '{sub(/.$/,"",$5);print $5}'
ns1.yahoo.com
ghostdog74