[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] [cut] Treat consecutive delimiters as one in cut binary
From: |
Bob Proulx |
Subject: |
Re: [PATCH] [cut] Treat consecutive delimiters as one in cut binary |
Date: |
Fri, 12 Mar 2004 23:55:33 -0700 |
User-agent: |
Mutt/1.3.28i |
Joe Maimon wrote:
> Jim Meyering wrote:
> >Joe Maimon <address@hidden> wrote:
> >>witness this script:
> >>dig -x $addr +noall +answer | grep -v '^;' | grep -v '$^' | expand | cut
> >>-d' ' -f5 -z
Ew, please don't word wrap commands. Examples should be verbatim.
Double "Ew" on the style. :-)
I also assume you meant "grep -v '^$'" too.
> >How about filtering the output through tr -s ' ' just before cut?
> >Then your script won't have to rely on less-portable features.
Even better use awk!
So that all can follow along here is what dig produces in this example.
dig -x 192.0.34.166 +noall +answer
;;;; <<>> DiG 9.2.3 <<>> -x 192.0.34.166 +noall +answer
;; global options: printcmd
166.34.0.192.in-addr.arpa. 14400 IN PTR www.example.com.
Awk is one of the standard utilities. It contains many features
better suited to processing text such as this.
dig -x $addr +noall +answer | grep -v -e '^;' -e '^$' | awk '{print$5}'
A feature of awk is NF. NF is the number of fields on the line.
$<some number> is that particular field. $NF is the last field in the
line. This is a very common idiom in awk. Knowing that dig is
printing this in column five is fine. A common way of doing this
would be to use $NF to print the last field on the line.
dig -x $addr +noall +answer | grep -v -e '^;' -e '^$' | awk '{print$NF}'
Here I only needed to use one grep because I could give it multiple
patterns with the -e options. Also notice that you don't need the
'expand' since awk splits on general whitespace. And since awk has
built in grep-like capability we can get rid of the grep.
dig -x $addr +noall +answer | awk '/PTR/{print$NF}'
Five processes down to two processes. Much simpler too. And since we
are now only paying attention to the line we care about we don't need
to avoid all of the other data. It is avoided automatically. This is
even simpler.
dig -x $addr | awk '/PTR/{print$NF}'
If you have the BIND 'dig' then you should also have the BIND 'host'
command which has a simple interface too. Personally I prefer the
host command for doing these types of things.
host -t ptr 192.0.34.166
166.34.0.192.in-addr.arpa domain name pointer www.example.com.
host $addr | awk '{print$NF}'
But wait, there's more. If you use a recent version of BIND then you
can use +short instead and avoid all of the other uninteresting
information.
dig -x 192.0.34.166 +short
www.example.com.
Nothing to do at all in that case.
> tr is usefull. And I would certainly use that on scripts that dont
> remain local. But this is a trivial to add feature. And it easily works
> for whatever delimiter is in use.
But awk is the better tool for this task whether the script is local
or not. It has just the right behavior to make splitting on
whitespace trivial.
Bob