ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to or want to delete BIND DNS entries that are in File1 that match A records in File2. If the DNS entry has multiple records associated to it in File2, those need to remove too.
car A 192.168.10.1
A 192.168.10.10
A 192.168.10.20
truck NS gtm1
NS gtm2
bike A 192.168.10.5
CNAME trek.example.com
After the removal, I need or would like to add back new DNS entries into File2.
Code:
car.example.com A 172.10.1.12
truck.example.com A 172.10.1.15
bike.example.com A 172.10.2.20
I tried a few grep. awk commands etc. I was able to remove the entry but not if the entry had multiple entries associated to it. However when I tried to start named.service it would fail.
Code:
grep -vwf file1 file2
Code:
grep -f <(cat file1 | sed 's/^/^/' ) file2
Last edited by dupa; 11-28-2021 at 09:07 AM.
Reason: Fixed code layout
awk '{
if ($0!~"^[ \t].*" ) printf("\n")
else printf("---newlinemark---")
printf("%s", $0)
} END {printf("\n")}' file2 >file_tmp
ee=($(cut -d '.' -f 1 file1))
echo ${ee[@]}
for i in ${ee[@]} ; do aa="$aa -e \"^$i\""; done
echo "$aa" |(xargs grep -v $aa file_tmp )| sed 's/---newlinemark---/\n/g' >out
echo "$aa" |(xargs grep $aa file_tmp )| sed 's/---newlinemark---/\n/g' >deleted
rm file_tmp
What it does is:
1) joins all lines of the same record in a temporary file (awk line)
2) reads file1 into a string and prepares the grep arguments: -e "^car" -e "^truck" -e "^bike"
3) writes all records without car truck bike recovering the newlines into the file out
4) writes the removed records into the file deleted
5) removes temporary file
replace ---newlinemark--- with something you will not find in any record
Last edited by Racho; 11-28-2021 at 10:32 AM.
Reason: improving code explanation
awk '{
if ($0!~"^[ \t].*" ) printf("\n")
else printf("---newlinemark---")
printf("%s", $0)
} END {printf("\n")}' file2 >file_tmp
ee=($(cut -d '.' -f 1 file1))
echo ${ee[@]}
for i in ${ee[@]} ; do aa="$aa -e \"^$i\""; done
echo "$aa" |(xargs grep -v $aa file_tmp )| sed 's/---newlinemark---/\n/g' >out
echo "$aa" |(xargs grep $aa file_tmp )| sed 's/---newlinemark---/\n/g' >deleted
rm file_tmp
What it does is:
1) joins all lines of the same record in a temporary file (awk line)
2) reads file1 into a string and prepares the grep arguments: -e "^car" -e "^truck" -e "^bike"
3) writes all records without car truck bike recovering the newlines into the file out
4) writes the removed records into the file deleted
5) removes temporary file
replace ---newlinemark--- with something you will not find in any record
interesting approach. Thanks! I did notice that it did remove more than I wanted though. anything that started with car or truck or bike removed.
example: car1, car300, bikes, etc.
Im trying to just remove the ones that are named exactly the same, nothing more.
Sorry, I don't get what your problem is. You're matching the records not against IP addresses, but against host names, right? So what if a host has multiple IP addresses? It will be matched anyway.
E.g.
Code:
$ dig de.archive.ubuntu.com|grep ^\\w
de.archive.ubuntu.com. 597 IN CNAME ubuntu.mirror.tudos.de.
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.23
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.24
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.22
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.26
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.25
If you're matching against ubuntu.mirror.tudos.de, it will match all records.
It would be nice if you provided an example of what you tried to do, and what didn't work.
Sorry, I don't get what your problem is. You're matching the records not against IP addresses, but against host names, right? So what if a host has multiple IP addresses? It will be matched anyway.
E.g.
Code:
$ dig de.archive.ubuntu.com|grep ^\\w
de.archive.ubuntu.com. 597 IN CNAME ubuntu.mirror.tudos.de.
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.23
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.24
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.22
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.26
ubuntu.mirror.tudos.de. 92 IN A 141.30.62.25
If you're matching against ubuntu.mirror.tudos.de, it will match all records.
It would be nice if you provided an example of what you tried to do, and what didn't work.
good example. how would you remove all those entries at once?
I think if I dig for it first then remove it, that may do the trick.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.