0

I have a input file that contains DNS zone entries

; whatever.foo [000000]
$ORIGIN whatever.foo.
$TTL 8400
@       IN      SOA     ns1.whatever.foo.      admin.whatever.foo. (
2017101913 ; serial number
                    21600                    ; refresh
                    8400                     ; retry
                    1209600                 ; expire
                    8400                    ; ttl
                    )

xxx           IN    A    000.000.000.000
something-xxx IN    A    000.000.000.000
other-xxx     IN    A    000.000.000.000
else.xxx      IN    A    000.000.000.000

yyy           IN    A    000.000.000.001
something-yyy IN    A    000.000.000.001
other-yyy     IN    A    000.000.000.001
else.yyy      IN    A    000.000.000.001

How do I remove for example entries that belong to xxx subdomain domain?

Output should be

; whatever.foo [000000]
$ORIGIN whatever.foo.
$TTL 8400
@       IN      SOA     ns1.whatever.foo.      admin.whatever.foo. (
2017101913 ; serial number
                    21600                    ; refresh
                    8400                     ; retry
                    1209600                 ; expire
                    8400                    ; ttl
                    )

yyy           IN    A    000.000.000.001
something-yyy IN    A    000.000.000.001
other-yyy     IN    A    000.000.000.001
else.yyy      IN    A    000.000.000.001

I would like it to be in a bash script so I can call e.g.

delete_domain.sh xxx

Thanks.

4
  • looks overcomplicated. You could post the initial input content and the expected output so we could suggest the optimal solution Commented Oct 19, 2017 at 9:44
  • Didn't I? Initial input is a zone file with entries like shown above. The output is a zone file with those entries removed. Also see edit. Commented Oct 19, 2017 at 9:48
  • @RandomWhiteTrash Simply post the input file (or a part of it) and the expected output from that. Commented Oct 19, 2017 at 12:35
  • Ok... this is a bad day... question makes no sense as the code works - it was not being used. I will rewrite it so it fits the answer below :( My bad :( Commented Oct 19, 2017 at 12:45

2 Answers 2

1

One way:

Test file:

$ cat file
xxx           IN    A    000.000.000.000
else.xxx      IN    A    000.000.000.000
something-xxx IN    A    000.000.000.000
other-xxx     IN    A    000.000.000.000
abc           IN    A    000.000.000.000
$

Pattern file:

$ cat patterns
xxx
something-xxx
other-xxx
else.xxx
$

Awk option:

$ awk 'NR==FNR{a[$1];next}!(($1 in a) && $2=="IN" && $3=="A")' patterns file
abc           IN    A    000.000.000.000
$

First we load all the patterns to be removed into the associative array a. Then when the test file is processed, only those lines are printed whose 1st column is not a part of pattern and 2nd column is "IN" and 3rd column is "A".

Sign up to request clarification or add additional context in comments.

Comments

0

Just a grep will do:

grep -E -v 'xxx\s+IN\s+A' file

And to get rid of multiple empty lines, pipe that into:

cat -s

Put those in a bash script:

$ cat test.sh
#!/bin/bash

grep -E -v 'xxx\s+IN\s+A' $1 | cat -s

and call it with:

./test.sh file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.