Reputation: 594
I have been working to setup my own DNS server using FreeBSD and I have done almost everything.
In order to write a custom script for logging and monitoring different domains in the zones, I am using host -a -l mydomain.com
command, that displays all the sub domains in the give domain. I am writing the output of this command to a file using > output
.
The problem is, the output has some other data as well which is of no interest for me.
The output of this command have following sections,
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6149
;; flags: qr aa ra; QUERY: 1, ANSWER: 45, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;unix. IN AXFR
;; ANSWER SECTION:
abc.unix
xyz.unix
Received 1108 bytes from 192.168.82.1#53 in 0 ms
Is there anyway that I can simply read write the section between ;; ANSWER SECTION:
and Received 1108 bytes from 192.168.82.1#53 in 0 ms
to a file using simple terminal command ? I am reading about awk
but I don't understand how I can use it to print only certain part of the output of the command.
Expected output
abc.unix
xyz.unix
Thank you
Upvotes: 2
Views: 444
Reputation: 21965
This is a simple requirement where sed
could be used
hostname -a -l mydomain.com | sed -n '/;; ANSWER/{n;N;p;q}'
would do it.
Output
abc.unix
xyz.unix
The q
command is handy as it will stop processing once the required portion is printed.
Upvotes: 2
Reputation: 2868
Your are right the awk
command is very handy for this situation.
Give a try to this:
awk '/^;; ANSWER SECTION:/ , /^Received [0-9]* bytes from [0-9.#]* in [0-9.] ms/ {if ($0 !~ /^;; ANSWER SECTION:/ && $0 !~ /^Received [0-9]* bytes from [0-9.#]* in [0-9.] ms/) print}'
The test:
$ host -a -l mydomain.com | awk '/^;; ANSWER SECTION:/ , /^Received [0-9]* bytes from [0-9.#]* in [0-9.] ms/ {if ($0 !~ /^;; ANSWER SECTION:/ && $0 !~ /^Received [0-9]* bytes from [0-9.#]* in [0-9.] ms/) print}'
abc.unix
xyz.unix
Upvotes: 3
Reputation: 3055
In awk you could try this:
awk '$0~/;; ANSWER SECTION:/{a=1; next}
(a && $0 !~/^Received/)' input
The output for the given example
abc.unix
xyz.unix
The first line sets a=1
when the current line contains ";; ANSWER SECTION:". If a=1
and the current line does not start with "Received", then we print the entire line. (If you want to suppress empty lines, you could add && $0 != ""
to (a && $0 !~/^Received/)
)
In combination with your command host -a -l mydomain.com
:
host -a -l mydomain.com | awk '$0~/;; ANSWER SECTION:/{a=1; next}
(a && $0 !~/^Received/)'
Upvotes: 3