Code help with search script perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code help with search script perl
# 1  
Old 05-14-2009
Code help with search script perl

Hello,

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

Code:
#!/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

Last edited by richsark; 05-14-2009 at 09:44 AM.. Reason: clairifcation
# 2  
Old 05-15-2009
Any help ?? Smilie
# 3  
Old 05-15-2009
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]:

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

is that what is "broke" in your script?
# 4  
Old 05-15-2009
HI, sorta of Smilie

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
# 5  
Old 05-15-2009
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:

Code:
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.
# 6  
Old 05-16-2009
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
# 7  
Old 05-16-2009
The contents of both the files have been mildly altered so as to show the case when the search is successful.

Code:
$
$ 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using forward slash in search pattern in perl script

I have existing pattern in the perl script as: my $pattern = "^Line.*?:|^Errors*: |^SEVERE:.*?:|^Null pointer exception occurred"; and I wanted to include below keywords in my search pattern "I/O exception" and "FileNotFoundException"the problem is when I include my pattern like my... (5 Replies)
Discussion started by: ambarginni
5 Replies

2. Shell Programming and Scripting

perl code to search existing files

Hi, I have a string like: read_lib {$lib/a.lib $lib/b.lib $lib/c.lib ..... } Now, I want to search existence of all these *.lib files in $lib directory. Please suggest- how to do it. Thanks -rkg (2 Replies)
Discussion started by: rkg
2 Replies

3. UNIX and Linux Applications

Perl Script to read an excel file into an array and search in the UNIX directories

Hi, I want the Perl script with versions 5.8.2 and 5.8.5 starting with #!/usr/bin/perl The Perl program should read the excel file or text file line by line and taking into an array and search in the UNIX directories for reference file of .jsp or .js or .xsl with path .The Object names... (2 Replies)
Discussion started by: pasam
2 Replies

4. Shell Programming and Scripting

perl script to search n place a pattern

hi, i have one file as t1.txt as below hi hello welcome i want perl script to search for the pattern "abcd" in the file. if the pattern doesn't exist, i want to insert that pattern at the end of the same file t1.txt my o/p should be hi hllo welcome abcd thank you (3 Replies)
Discussion started by: roopa
3 Replies

5. Shell Programming and Scripting

Perl script to search and extract using wildcards.

Good evening All, I have a perl script to pull out all occurrences of a files beginning with xx and ending in .p. I will then loop through all 1K files in a directory. I can grep for xx*.p files but it gives me the entire line. I wish to output to a single colum with only the hits found. ... (3 Replies)
Discussion started by: CammyD
3 Replies

6. Shell Programming and Scripting

Perl script to search sprintf and replace with snprintf

Dear all, I am new to perl script and would need some help for my 1st script. I wrote a script to search sprintf(buf,"%s", sourcestring) and replace with snprintf(buf, sizeof(buf),"%s", sourcestring). As snprintf() requires an extra argument, so it is not a simple search-and-replace. I need to... (1 Reply)
Discussion started by: ChaMeN
1 Replies

7. UNIX for Dummies Questions & Answers

Perl search and replace not working in csh script

I am using perl to perform a search and replace. It works at the command line, but not in the csh shell script perl -pi -e 's@/Pattern@@g' $path/$file I used the @ as my delimiter because the pattern contains "/" (3 Replies)
Discussion started by: NobluesFDT
3 Replies

8. Shell Programming and Scripting

search & replace password perl script

I wanted a perl script to be done for Password search & replace in two files. For Example: Example 1)--i am having a file such as cat /opt/customer/Ariba/UAT/ariba/app/buyer/Server/config/Parameters.table Example 2)--and i am having a other file in other location such as cat... (4 Replies)
Discussion started by: shellscript22
4 Replies

9. Shell Programming and Scripting

Help with perl script to search webpage

i have to point out that i'm a avid shell script lover and i have next to zero interest in perl which is why i dont know the first thing about it. however, just need to do this one thing on it.. i need to do something in perl but dont know how. I have a list of email addresses in the following... (5 Replies)
Discussion started by: Terrible
5 Replies

10. Shell Programming and Scripting

Perl code to search for filenames that contain special characters

Hello, I have a requirement to search a directory, which contains any number of other directories for file names that contain special characters. directory structure DIR__ |__>DIR1 |__>DIR2__ |__>DIR2.1 |__>DIR2.2 |__>DIR3 .. ... (8 Replies)
Discussion started by: jerardfjay
8 Replies
Login or Register to Ask a Question