Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching a txt file.

Status
Not open for further replies.

IamStang

Technical User
Feb 12, 2005
27
0
0
US
Hi all,

I hope someone can help me out with this one as I have searched the web for litterally hours and have not found a clear cut answer.

Is it possible to have a cgi script "search" a txt file? Everything I am finding is "Sql" based. I would rather not use Sql if possible. I would rather have a simple text file broken down like this:

Code:
1965mustang | [URL unfurl="true"]http://www.mypage.com/65m.html[/URL]
1966mustang | [URL unfurl="true"]http://www.mypage.com/66m.html[/URL]
1967mustang | [URL unfurl="true"]http://www.mypage.com/67m.html[/URL]
1968mustang | [URL unfurl="true"]http://www.mypage.com/68m.html[/URL]
1969mustang | [URL unfurl="true"]http://www.mypage.com/69m.html[/URL]

(Of course, this txt file could eventually contain 100s, if not 1000s, of entries.)

Now, is it possible to do a search of this document from a form and have the cgi return the relevant link?

If so, maybe someone could point me in the direction of a good tutorial on this type of process. I am really only looking for a push in the right direction. However, if someone wants to give more than a push, I wont mind that either.

Thanks for reading!

IamStang
Technical User my butt!
Just another nooB!
 
should be simple but it depends on what the form is sending to the script as search parameters.

The file will be better off with no spaces:

1965mustang|1966mustang|1967mustang|1968mustang|1969mustang|
and the first part might need to be broken into two fields:

1965|mustang|1966|mustang|1967|mustang|1968|mustang|1969|mustang|
so if a person wants to search for: Mustang 1969

your script should use CGI.pm to fetch the form data and do something along these lines (assuming the file looks like my last example above):

Code:
#!perl
use strict
use CGI qw/:standard/;
print header();

my $first_param = param('first_param')
my $second_param = param('second_param');

my @results = ();
open (FILE, "path/to/data.txt") or die "$!";
while (<FILE>) {
   my @data = split(/\|/);
   push @results, $data[2] if ($first_param eq $data[0] && $second_param eq $data[1]);
}
close(FILE);
if (@results) {
   print "$_<br>\n" for @results;
}
else {
   print "No results for: $first_param $second_param<br>";
}

this is a really simple example and would need to be greatly expanded to work securely and accurately. But that is the basic concept, open the document, split the lines on the delimiter "|" and check for matches. Then you can build an array or hash of results and print the list of results. If there gets to be many results you may need to use pagination too.

Keep your data fields as specific and discrete as possible, don't mix dates and descriptions and so on. This way you can break data down to highly targeted results very easily.

make|model|year|color|condition|description|etc|etc|etc

of course this will never compete with a real relational databse such as MySQL or others.
 
Hi,

My €0.02 would be if you're going to using static .htm pages for each vehicle, why not have an indexer build your results based on the content of each .html file

ie. if a file contains 1969 *AND* mustang it'll come up in the search results

I'd love to explain in detail exactly what goes on, but these guys have done such a good job of it, I don't need to.

It means noone has to maintain indexes of files, as it's all done once a day by an automatic process

HTH
--Paul

cigless ...
 
First of all, I want to thank both of you for your input.

PaulTEG ... I will go check that link out as soon as I am done here.

KevinADC ... you said
this is a really simple example and would need to be greatly expanded to work securely and accurately.

You mentioned "securely". I am always interested in security. What makes this insecure?

[ol]
[li]The fact that it tells, in the script, the name of the txt file? (other than setting privlages for that file, I wouldn't have a clue how else to secure it)[/li]

[li]That, if using a form, anyone can, for lack of a better word, overload the cgi with text? (This I feel fairly comfortable in my ability to handle:) found a good website about that.)[/li]

[li]That someone could run malicious code in the form? (could be taken care of by stripping characters, right?)[/li]

[li]Or something I am not thinking about?[/li][/ol]

Thanks!
IamStang
 
Thats pretty much it. Make sure to use CGI.pm's built in security features and you should be OK: Turn off uploads so nobody can use the form to upload files to your server and limit the data. CGI.pm escapes html by default so that should not be a problem if you use CGI.pm to fetch the form data.

You can use the -T switch in the shebang line:

#!/usr/bin/perl -T

and see if you get any warnings.
 
Thank you folks very much!

I have posted, what some would consider, "lamer questions" on other boards and have been treated as such. This board seems to be the place to get help regardless of one's cgi coding experience.

I learn internet related coding on my own and will be sure to frequent this board for answers I cant find on my own. I prefer to figure it out on my own, but sometimes I find information that points me in the wrong direction and find myself needing help.

Again, thank you all.

IamStang
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top