LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   plz help me to compare (https://www.linuxquestions.org/questions/programming-9/plz-help-me-to-compare-551112/)

reaky 05-03-2007 06:11 PM

plz help me to compare
 
Dear all
I want to compare the zones in both my Linux Bind servers, So I greped the first line of all of them with
#> grep 'zone "' named.conf > named.1
and the same for named.conf 2
now I get two files named.1 named.2, with the following syntax
zone "domain1.com" in {
zone "domain2.com" in {
zone "domain3.cc" in {
zone "domain4.net" in {
zone "domain5.tv" in {
etc..

First Can any one tell me a grep command to just get the domains name between the "" in a file
second I want to compare between the both files and get the domains that in named.1 and not found in named.2

Tinkster 05-03-2007 07:09 PM

Not tested:

Code:

egrep -o '"[^"]+"' file1 | sed 's/"//g'  > comp1
egrep -o '"[^"]+"' file2 | sed 's/"//g' > comp2
sdiff -s comp1 comp2


Cheers,
Tink

reaky 05-03-2007 07:46 PM

Thanx so much
the first command worked perfectly
But there's something in the second one
It compared with two files as they are arranged like each others, But they don't
For example name1
zone "domain1.com" in {
zone "domain2.com" in {
zone "domain3.cc" in {
zone "domain4.net" in {
zone "domain5.tv" in {
etc..

But named2
zone "domain5.tv" in {
zone "domain3.cc" in {
zone "domain2.com" in {
zone "domain4.net" in {
zone "domain1.com" in {
etc..

I just want it to get the domains that are in one of them and not in the other, To add it to the other one, Got it.

Thanx for your help.

ntubski 05-03-2007 09:40 PM

Quote:

Originally Posted by reaky
I just want it to get the domains that are in one of them and not in the other, To add it to the other one, Got it.

how about:
Code:

$ cat name1 name2 |sort |uniq > allnames
$ cat allnames
zone "domain1.com" in {
zone "domain2.com" in {
zone "domain3.cc" in {
zone "domain4.net" in {
zone "domain5.tv" in {

Of course, if the original order is important to you this might not be so great...

Tinkster 05-03-2007 10:39 PM

Code:

egrep -o '"[^"]+"' file1 | sed 's/"//g' | sort  > comp1
egrep -o '"[^"]+"' file2 | sed 's/"//g' | sort  > comp2
sdiff -s --left-column comp1 comp2
sdiff -s --left-column comp2 comp1

How's that?


Cheers,
Tink

reaky 05-03-2007 10:57 PM

No I don't want to arrange them, I just want to compare.
Thanx for help

jschiwal 05-04-2007 12:27 AM

I recently had to prepare scripts that extracted the names of mpeg files in several video devices against one of the devices. I used the "comm" command to find entries in one list that were unique, and then used that list as a grep pattern file to the original.
Code:

$ cat rdytospot
#!/bin/bash
for file in *.RDY; do
  sed -e '0,/Unmanaged/d' -e '/NETTEST/d' -e '/^$/,$d' -e '/^$/d' -e 's/\(.*MPG\).*/\1/' -e 's/[[:blank:]]//g' "$file"
done > unmanagedspots
sort -u unmanagedspots >unmsorted
comm -23 unmsorted solicitorlist >deletelist
if grep -f deletelist solicitorlist; then
  echo Error!  The delete list contains files in the solicitor
  exit 1
fi
sed 's/^/del /' deletelist >clean.dvc
rm unmanagedspots unmsorted deletelist

You could do something similar:
Code:


sed -n '/^zone/s/^[^"]*"\([^"]*\)".*/\1/p' named2 | sort >named2list
sed -n '/^zone/s/^[^"]*"\([^"]*\)".*/\1/p' named1 | sort >named1list
comm -13 named1list named2list >UniqInNamed2
grep -f UniqInNamed2 named2

By the way, your two samples have exactly the same domain names, just in a different order.
Using different samples:
$ cat named1
zone "domain1.com" in {
zone "domain2.com" in {
zone "domain3.cc" in {
zone "domain4.net" in {
zone "domain5.tv" in {

$ cat named2
zone "domain8.tv" in {
zone "domain3.cc" in {
zone "domain7.com" in {
zone "domain4.net" in {
zone "domain1.com" in {

$ grep -f UniqInNamed2 named2
zone "domain8.tv" in {
zone "domain7.com" in {

Tinkster 05-04-2007 01:53 AM

Quote:

Originally Posted by reaky
No I don't want to arrange them, I just want to compare.
Thanx for help

Sorry mate, w/o sorting the diff output will be
garbage. With the sorting you get two sets that
(most likely) won't appear in either, and then
you could feed them back. If that's not what
you're after *I* can't help you.



Cheers,
Tink

jim mcnamara 05-04-2007 10:59 AM

try this:
Code:

# system 1
grep '^zone "' named.conf | awk '{print $2}' | tr -s '"' ' ' > file1
# system 2
grep '^zone "' named.conf | awk '{print $2}' | tr -s '"' ' ' > file2
awk ' FILENAME=="file1" { arr[$0]=1}
      FILENAME=="file2" {
          if( !arr[$0]) { print $0 }
      }
    ' file1 file2


reaky 05-04-2007 01:01 PM

Dear all
Thanx so much ,You really helped me so much
THax all again. :)


All times are GMT -5. The time now is 09:55 PM.