Code help with search script perl

Hello,

I have a script, but its sorta broke and I need help to tweak it

#!/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);
}

The above script takes 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.,,,,

from the above example we take one record (bd-integ4-sarkprime,IN,NS,gss-t2i.is.sark.com.,,,,) ( for example) strip it and it should result as:

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)

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

Then I want it to search inside 3 files for any matches to bd-integ4-sarkprime and gss-t2i.is.sark.com to report on it. 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,,

For "exportOBJ-sark.txt" I can remove the ",,," manually before running the script so it can join as cli2821cncx-stmin-00-01.is.sark.com, or if the script can do that?

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,"",""

Can I get some help to tune this up please.

Thanks

Any help ?? :confused:

if you have this line:

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

and split in on the commas you will have this:

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

note that 'bd-integ4-sarkprime' is index zero [0] and 'gss-t2i.is.sark.com' is index three [3], but in your code above you are using index four [4]:

  $search_hash{$parts[0]} = 1;
  $search_hash{$parts[4]} = 1;

is that what is "broke" in your script?

HI, sorta of :slight_smile:

All I need to do for the first part is from this:
bd-integ4-sarkprime,IN,NS,gss-t2i.is.sark.com.,,,

to be like:
bd-integ4-sarkprime
and another search as:
gss-t2i.is.sark.com.

Then to take the above and look for a match in the following files:

exportOBJ-sark.txt","ResourceRecText-ALL.txt","exportdnsrr-report.txt

Keeping in mind the the above files has different syntax and format that the
script needs to take in consideration.

Thanks

hard to say because in the files you want to search, there are no matches for the search terms at all.

here are the files to search:

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.

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,"",""

here are you sample search terms:

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

I don't see either of those search terms in the search files. I see a partial match of 'is.sark.com' but you did not say you want to find partial matches. So unless you can clarify better I don't want to spend any time trying to help.

Hello Kevin,

Thats my issue,the script needs some tweaking to forfill my needs.

I need a search to include the full name ( not just is.sark.com) as such from my example:

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

stripped to:
bd-integ4-sarkprime

and another search as:
gss-t2i.is.sark.com.

The script needs to be smart enough to accomplish this. If you have another idea, please advise.

I would appericate your help on a script that will do the needful.

Thanks

The contents of both the files have been mildly altered so as to show the case when the search is successful.

$
$ cat sark.com.txt
ao,IN,CNAME,www.sark.com.,,,
bd-integ4-sarkprime,IN,NS,gss-t2i.is.sark.com.,,,,
sarkhub,IN,MX,sarkhub1,,,
bpm,IN,NS,gss-t2p.is.sark.com.,,,,
bpmqa,IN,NS,gss-t2i.is.sark.com.,,,,
$
$ cat exportdnsrr-report.txt
srsservicing.net,sarkprivate.srsservicing.net,IN,sarkhub,A,-1,100.168.67.190,,,0,0,"",www.sark.com.
srsservicing.net,sarkmanagedcontrolprivate.srsservicing.net,IN,A,-1,100.168.67.189,,,0,0,"",""
$
$ perl -ne 'BEGIN{open(F,"sark.com.txt"); while(<F>) {chomp; split/,/; $s{$_[0]}=1; $s{$_[3]}=1}}
> {chomp; @x=split/,/; foreach $i(@x){if (defined $s{$i}) {print $_,"\n"; last} }}' exportdnsrr-report.txt
srsservicing.net,sarkprivate.srsservicing.net,IN,sarkhub,A,-1,100.168.67.190,,,0,0,"",www.sark.com.
$

tyler_durden

HI,

I saw your one example on
exportdnsrr-report.txt how about the other files/formats?

Thanks !

Depends on the delimiter and what exactly you want to match. The code assumes that:

(1) Your delimiter is a comma, and
(2) You want to match an entire token between delimiters i.e. this -

123,XYZ,abc,0.234

is a valid match for "abc", but this -

123,XYZ,param=abc,0.234

is not.

Since the delimiters for "exportOBJ-sark.txt" and "exportdnsrr-report.txt" are the same (comma), the code should work for both, assuming you want the match semantics like (2) above.

For the 3rd file - "ResourceRecText-ALL.txt", you'll have to split it as per the delimiter. And the rest of the logic remains the same.

tyler_durden

Hi Durden,

Thanks for your reply. Would it be possiable to provide me a code for the last one?

"For the 3rd file - "ResourceRecText-ALL.txt", you'll have to split it as per the delimiter. And the rest of the logic remains the same"

I hate to be a burden, but I do not have the fore knowledge.

Also, I asumme I have to run this on the command line, and I get convert it to a script right?

Thanks !

What's the delimiter in the 3rd file - "ResourceRecText-ALL.txt" ?

You can run it on the command line passing one file at a time.
Or you can incorporate the logic in a perl program that processes all your files.

tyler_durden

Hi !

for 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.

I am looking to search only on what the name is after the =

so for the abve excerpt would be:
whdgss1infz-qapri1.is.sark.com.

and

n3mgss1infz-qasec1.is.sark.com.

Thanks

Does that help ?

HI, I ran your slightly modified code, and I got the same response.

When I run it on my actual files, I dont get the same..

$ perl -ne 'BEGIN{open(F,sark.com.txt"); while(<F>) {chomp; split/,/; $s{$_[0]}=1; $s{$_[3]}=1}}

> {chomp; @x=split/,/; foreach $i(@x){if (defined $s{$i}) {print $_,"\n"; last} }}'exportOBJ-sark.txt

String found where operator expected at -e line 2, near "{chomp; @x=split/,/; foreach $i(@x){if (defined $s{$i}) {print $,""
(Might be a runaway multi-line "" string starting on line 1)
(Missing semicolon on previous line?)
String found where operator expected at -e line 2, at end of line
(Missing semicolon on previous line?)
syntax error at -e line 2, near "{chomp; @x=split/,/; foreach $i(@x){if (defined $s{$i}) {print $
,""
Can't find string terminator '"' anywhere before EOF at -e line 2.