|
perl to search a file |
This is a simple perl to search a text file on your server The features. What it needs to work properly. Example. The script itself. Further info. Navigation. |
![]() |
Features |
![]() |
|
![]() |
Again it's really simple. The path and filename of the data to be searched are hardcoded. The perl reads parameters whether they come via GET or POST. There is only one parameter supported: you can specify the search term. | ![]() |
|
![]() |
The perl lives on a server and is accessed via an url. Any programm that can read data from an url can do this, a browser or a director application. When called the perl searches the data and reports the success. | ||
![]() |
Needs |
||
![]() |
Adapt the path and filename to your situation. | ||
![]() |
Upload the perl with ftp as TEXT to your servers cgi-bin directory. | ||
![]() |
Set the file permission with chmod to 755. | ||
![]() |
The Script: |
![]() |
#!/usr/bin/perl ############################################################## # # dp_tiny_search.pl 19.11.98 # # -------------------------------- # Daniel Plaenitz # contact(AT)lingo.de # www.lingo.de/ego.htm # ############################################################## # # daten kommen per get im query_string: # tiny_search?term=MySearchExpression # # ############################################################## # Define Variables # $pathname = "./database/"; $filename = "highscore.txt"; # ############################################################## # $filename = $pathname.$filename; # Done ############################################################## # # # schnapp dir die parameter &Parse; # # lies die Daten # &DateiEin; # # Suche und Ausgabe # &search; exit(1); # # Fertig! # ################################################################ # # # subroutines # # sub Parse { local(@pairs,$pair,$val,$pos, $i); if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $in, $ENV{'CONTENT_LENGTH'}+1); } $i = 0; @pairs = split(/&/, $in); foreach $pair (@pairs) { # do the special character conversion $pair =~ tr/+/ /; $pair =~ s/%(..)/pack("c",hex($1))/ge; $pos = index($pair,"="); $name = substr($pair,0,$pos); $val = substr($pair,$pos+1); $i++; $entry{$name} = $val; } return 1; } ############################################################ # sub DateiEin { open(FILE,"$filename"); @lines = <FILE>; close(FILE); } ############################################################ # sub search { # Header voraussenden print "Content-type: text/html\n\n"; # do we have a valid search term? if ($entry{'term'} eq "") { print "no search term transmitted!"; exit(1); } # der zaehler $lineNo = 0; # indikator $found=""; # je zeile foreach $line (@lines) { # increment line counter $lineNo++; # does this line contain the term? if ($line =~ /$entry{'term'}/) { $found = "found \"$entry{'term'}\" at line $lineNo of file $filename <br>\n"; last; } } # report if ($found eq "") { $found = "sorry, \"$entry{'term'}\" could not be found. <br>\n"; } print $found; return 1; } # ###############################################################
pageID=tinySearch" border ="0" >
Daniel Plänitz
17.12.98, 21.5.00