• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

search script request

Hello,

I am in need of a script that will take an input file named sark.com.txt which contains this type of info:

ao,300,IN,CNAME,www.sark.com.,,,
bd-integ4-sarkprime,IN,NS,gss-t2i.is.sark.com.,,,,
sarkhub,IN,MX,5,sarkhub1,,,
bpm,IN,NS,gss-t2p.is.sark.com.,,,,
bpmqa,IN,NS,gss-t2i.is.sark.com.,,,,

(BTW..The format above will not change.)

Then I want it to search inside 3 files called
exportOBJ-sark.txt
(example of contents)
10.0.234.254,cli2821cncx-stmin-00-01,,,is.sark.com,Server,"",,,,,-1,0,,,-1,,3,,
10.0.237.254,cli2821cncx-tgsp-00-01,,,is.sark.com,Server,"",,,,,-1,0,,,-1,,3,,

ResourceRecText-ALL.txt
(example of contents)
outputRR-sark.com.txt:ResourceRecText=whdgss1infz-qapri1.is.sark.com.
outputRR-sark.com.txt:ResourceRecText=n3mgss1infz-qasec1.is.sark.com.

exportdnsrr-report.txt
(example of contents)
srsservicing.net,sarkprivate.srsservicing.net,IN,A,-1,100.168.67.190,,,0,0,"",""
srsservicing.net,sarkmanagedcontrolprivate.srsservicing.net,IN,A,-1,100.168.67.189,,,0,0,"",""


For any hits meaning matching names or any name match that is found in these 3 files above to report on it.

The overall goal is to see if the names from sark.com.txt are found anywhere else in these 3 files.

Thanks
0
richsark
Asked:
richsark
  • 17
  • 5
  • 2
  • +1
1 Solution
 
Bryan ButlerCommented:
Something like this?

for each line in sark.com.txt
    parse line to get "names" (need better definition of what exactly is a "name")
    for each name found in the line
        search through the 3 files and if any lines have the "name", output the filename and line
0
 
richsarkAuthor Commented:
Hi,

A name would be categorized as bd-integ4-sarkprime and or gss-t2i.is.sark.com. Another example is:
sarkhub and sarkhub1 which are taken from the sark.com.txt file

ao,300,IN,CNAME,www.sark.com.,,,
bd-integ4-sarkprime,IN,NS,gss-t2i.is.sark.com.,,,,
sarkhub,IN,MX,5,sarkhub1,,,
bpm,IN,NS,gss-t2p.is.sark.com.,,,,
bpmqa,IN,NS,gss-t2i.is.sark.com.,,,,

Does this help?
0
 
Bryan ButlerCommented:
Seems like it would be hard to come up with a rule to determine if a particular parsed token is a "name" or not (I'm assuming tokens are comma separated).  I guess you could just check every token, but then things like "IN" would show up in your examples above.  Is there any rules to determine a "name"?  Or is there rules to determine what's not a name?  If you can work this out, then what I pseudocoded should work.  I can crank up the old perl brain cells and give you some code if you want, but the "name" thing needs to be worked out first.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
richsarkAuthor Commented:
ok, lets do it like this:

for this example:
bd-integ4-sarkprime,IN,NS,gss-t2i.is.sark.com.,,,,

from that we use:

bd-integ4-sarkprime
gss-t2i.is.sark.com

for this:
sarkhub,IN,MX,5,sarkhub1,,,

lets use:
sarkhub
sarkhub1

you follow me?


0
 
bersekenCommented:
Something like this? :)

======================

#!/usr/bin/perl

my @files_to_search = ("exportOBJ-sark.txt","ResourceRecText-ALL.txt","exportdnsrr-report.txt");

open (FL, "sark.com.txt");
my %search_hash = ();
while (my $line = <FL>) {
  my @parts = split(/,/,$line);
  $search_hash{$parts[0]} = 1;
  $search_hash{$parts[4]} = 1;
}
close(FL);

foreach my $file (@files_to_search) {
  open (FL, $file);
  while (FL) {
     foreach my $key (keys %search_hash) {
       if ($_ =~ /$key/) {
          print $_;
       }
     }
  }
  close(FL);
}
0
 
richsarkAuthor Commented:
HI,

Let me give it a shot
0
 
richsarkAuthor Commented:
HI, got this message

Use of uninitialized value $_ in pattern match (m//) at ./compare.pl line 18.
0
 
bersekenCommented:
ok.. try this:

#!/usr/bin/perl

my @files_to_search = ("exportOBJ-sark.txt","ResourceRecText-ALL.txt","exportdnsrr-report.txt");

open (FL, "sark.com.txt");
my %search_hash = ();
while (my $line = <FL>) {
  my @parts = split(/,/,$line);
  $search_hash{$parts[0]} = 1;
  $search_hash{$parts[4]} = 1;
}
close(FL);

foreach my $file (@files_to_search) {
  open (FL, $file);
  while (my $line = <FL>) {
     foreach my $key (keys %search_hash) {
       if ($line =~ /$key/) {
          print $line;
       }
     }
  }
  close(FL);
}
0
 
richsarkAuthor Commented:
I tried the above, now I get

$ ./compare.pl
./compare.pl: line 1: $'\r': command not found
./compare.pl: line 3: $'\r': command not found
./compare.pl: line 4: syntax error near unexpected token `('
'/compare.pl: line 4: `my @files_to_search = ("exportOBJ-sark.txt","ResourceRecText-ALL.txt","exportdnsrr-report.txt");


0
 
richsarkAuthor Commented:
My Mistake,

It worked, but I get tons of responses like below: Need to know why there are so many matches??


10.0.234.254,cli2821cncx-stmin-00-01,,,is.sark.com,Server,"",,,,,-1,0,,,-1,,3,,
10.0.234.254,cli2821cncx-stmin-00-01,,,is.sark.com,Server,"",,,,,-1,0,,,-1,,3,,
10.0.237.254,cli2821cncx-tgsp-00-01,,,is.sark.com,Server,"",,,,,-1,0,,,-1,,3,,
outputRR-sark.com.txt:ResourceRecText=whdgss1infz-qapri1.is.sark.com.
outputRR-sark.com.txt:ResourceRecText=n3mgss1infz-qasec1.is.sark.com.
srstservicing.net,sarkprivate.srsservicing.net,IN,A,-1,192.168.67.190,,,0,0,"",""
srstservicing.net,sarkprivate.srsservicing.net,IN,A,-1,192.168.67.190,,,0,0,"",""
0
 
richsarkAuthor Commented:
HI, I did some trouble shooting, I had the input file with just one entry, Then I ran the above code, it still found tons of unrelated  names.  I think its looking for to many wild cards.

Perhaps we can step through the code to isolate why its finding so many names that do not even match.

Thanks !
0
 
bersekenCommented:
can't tell based on your output.. probably one of the search strings in the sark.com.txt file is really generic and matches a bunch of lines in the search files.

for example, each line in your output has 'sark' in it. So, if one of your search strings is 'sark' then it will match all these lines.
0
 
richsarkAuthor Commented:
Hi, your right, If my sark.com.txt has only this line:

bd-integ4-sarkprime,IN,NS,gss-t2i.is.sark.com.,,,,

from that we use:

bd-integ4-sarkprime
and
gss-t2i.is.sark.com

The script should then look for bd-integ4-sarkprime and gss-t2i.is.sark.com in the 3 files (exportOBJ-sark.txt","ResourceRecText-ALL.txt","exportdnsrr-report.txt)

for any matches to bd-integ4-sarkprime and gss-t2i.is.sark.com

Is that what its doing?

if not, can we make it so?

Thanks
0
 
richsarkAuthor Commented:
Hi,

Any follow ups?
0
 
bersekenCommented:
ok. so it almost does that.  

  $search_hash{$parts[4]} = 1;
should be:
  $search_hash{$parts[3]} = 1;

#!/usr/bin/perl

my @files_to_search = ("exportOBJ-sark.txt","ResourceRecText-ALL.txt","exportdnsrr-report.txt");

open (FL, "sark.com.txt");
my %search_hash = ();
while (my $line = <FL>) {
  my @parts = split(/,/,$line);
  $search_hash{$parts[0]} = 1;
  $search_hash{$parts[3]} = 1;
}
close(FL);

foreach my $file (@files_to_search) {
  open (FL, $file);
  while (FL) {
     foreach my $key (keys %search_hash) {
       if ($_ =~ /$key/) {
          print $_;
       }
     }
  }
  close(FL);
}
0
 
richsarkAuthor Commented:
HI, Thanks for the update,

I now get this:

$ ./compare.pl
Use of uninitialized value $_ in pattern match (m//) at ./compare.pl line 20.
0
 
richsarkAuthor Commented:
Hi,

Just following up
0
 
richsarkAuthor Commented:
Hello, Anyone want to help a brotha' out?

I am in need of an expert
0
 
bersekenCommented:
#!/usr/bin/perl

my @files_to_search = ("exportOBJ-sark.txt","ResourceRecText-ALL.txt","exportdnsrr-report.txt");

open (FL, "sark.com.txt");
my %search_hash = ();
while (my $line = <FL>) {
  my @parts = split(/,/,$line);
  $search_hash{$parts[0]} = 1;
  $search_hash{$parts[3]} = 1;
}
close(FL);

foreach my $file (@files_to_search) {
  open (FL, $file);
  while (my $line = <FL>) {
     foreach my $key (keys %search_hash) {
       if ($line =~ /$key/) {
          print $line;
       }
     }
  }
  close(FL);
}
0
 
richsarkAuthor Commented:
Hi,
Thanks will try it in a few. I have been ill
Thanks
 
0
 
richsarkAuthor Commented:
HI, I am not sure whats going on...

When I run it, I get tons of scrolling lines, and no matching, reporting on nothing.

any thoughts? or advisement or  an alternative way?


0
 
ahoffmannCommented:
egrep '(bd-integ4-sarkprime|gss-t2i\.is\.sark\.com|sarkhub|sarkhub1)' exportOBJ-sark.txt ResourceRecText-ALL.txt exportdnsrr-report.txt

KISS - keep it simple stupid
0
 
richsarkAuthor Commented:
Hello,

Thanks for the update, however, your example means that I have to run them one at a time. I need to make it automatic.

Thanks
0
 
ahoffmannCommented:
> .. make it automatic.
whatever this means, but how about simply stuffing that command into a crontab?
0
 
richsarkAuthor Commented:
HI :)

I meant to use this script to do the job but to fix it first.



#!/usr/bin/perl
 
my @files_to_search = ("exportOBJ-sark.txt","ResourceRecText-ALL.txt","exportdnsrr-report.txt");
 
open (FL, "sark.com.txt");
my %search_hash = ();
while (my $line = <FL>) {
  my @parts = split(/,/,$line);
  $search_hash{$parts[0]} = 1;
  $search_hash{$parts[3]} = 1;
}
close(FL);
 
foreach my $file (@files_to_search) {
  open (FL, $file);
  while (my $line = <FL>) {
     foreach my $key (keys %search_hash) {
       if ($line =~ /$key/) {
          print $line;
       }
     }
  }
  close(FL);
}

Open in new window

0
 
richsarkAuthor Commented:
Hello,

Just following up with the above script to make it work per my requirement on the top of the thread.

Thanks in advance !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 17
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now