Perl search multiple fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl search multiple fields
# 1  
Old 03-25-2013
Perl search multiple fields

Hi all,

I have a flatfile 300 lines
Code:
tiger,tampa10-pc,yellow,none,2013-02-25 08:56:51.000,2013-02-25 21:41:11.380,12hrs : 44min
cat,tampa10-pc,white,none,2013-02-28 08:56:58.000,2013-03-04 23:18:23.003,110hrs : 21min
dog,tampa10-pc,yellow,none,2013-03-05 09:50:17.000,2013-03-07 00:08:02.617,38hrs : 17min
dog,tampa10-pc,yellow,Trainned,2013-02-24 00:00:00.000,2013-02-24 22:34:10.293,22hrs : 34min
dog,sanjose10-pc,yellow,Trainned,2013-02-24 22:34:32.000,2013-02-25 08:56:58.743,10hrs : 22min
dog,sanjoseis10-pc,yellow,Unknown,2013-02-25 21:40:21.000,2013-02-27 00:17:36.007,26hrs : 37min
dog,sanjose10-pc,yellow,Unknown,2013-02-27 00:17:57.000,2013-02-28 00:19:35.340,24hrs : 01min
dog,denver10-pc,black,Unknown,2013-02-28 00:19:38.000,2013-02-28 08:57:12.103,8hrs : 37min
dog,denver10-pc,black,Trainned,2013-03-04 23:18:16.000,2013-03-05 09:50:25.500,10hrs : 32min
dog,denver12-pc,yellow,Unknown,2013-03-07 00:07:51.000,2013-03-13 00:04:35.213,143hrs : 56min
dog,denver10-pc,yellow,Unknown,2013-03-13 00:04:48.000,2013-03-14 01:24:15.437,25hrs : 19min
dog,denver12-pc,yellow,Unknown,2013-03-14 01:24:37.000,2013-03-15 10:23:17.503,32hrs : 58min

I created a basic html form:
Code:
 <html><head> <TITLE>Search </TITLE>
 <script language="javascript" type="text/javascript" src="./applets/datetimepicker_css.js">
</script>
</head>

 <body>
<center><h1>Search</h1></center>
<hr WIDTH="80%">
<FORM METHOD="POST" ACTION="search.pl">
<table  border=0 WIDTH="80%"cellspacing=4 cellpadding=3 bordercolor="#FFCC00" style="background-color:#FFFFCC">
 <tr >
  <td >
  Start Date:
  </td>
  <td >
<input type="text" id="startdate" size="16"> <img src="./images/cal.gif" width="16" height="16" border="0" onclick="javascript:NewCssCal('startdate','yyyyMMdd')" style="cursor:pointer"/>
  </td>
  <td >
  End Date:
  </td>

  <td >
<input type="text" id="enddate" size="16"> <img src="./images/cal.gif" width="16" height="16" border="0" onclick="javascript:NewCssCal('enddate','yyyyMMdd')" style="cursor:pointer"/>
  </td>
 </tr>
 <tr >
  <td >
  Animal
  </td>

  <td >
 Location:
  </td>
  <td >
  Color:
  </td>
  <td >
  Trainning:
  </td>
 </tr>
 <tr >

  </td>
  <td >
  <SELECT NAME="animal" SIZE="1">
    <OPTION SELECTED>all
    <OPTION>tiger
     <OPTION>cat
    <OPTION>dog
    
</SELECT>
  </td>
  <td >
  <SELECT NAME="location" SIZE="1">
  <OPTION SELECTED>all
  <OPTION>tampa
   <OPTION>sanjose
  <OPTION>denver
</SELECT>
  </td>
  <td>
<SELECT NAME="color" SIZE="1">
  <OPTION SELECTED>all
  <OPTION>black
   <OPTION>yellow
  <OPTION>white

</SELECT>
  </td>
    </td>
    <td>
  <SELECT NAME="training" SIZE="1">
    <OPTION SELECTED>Unknown
     <OPTION>Traiined
    <OPTION>none
  
</SELECT>
  </td>
 </tr>
</table>
 <HR WIDTH="80%">

<INPUT TYPE="Reset" VALUE="Clear Form">
<INPUT TYPE="Submit" VALUE="Submit">
</FORM>

</body></html>

How do i write search.pl so when i select startdate : 2013-03-15 and enddate : 2013-03-24 AND select option animal AND select option Location AND select option Color AND select option training then SUBMIT it will search for me.

I cannot use sql because my system does not have it.
Thanks
# 2  
Old 03-26-2013
Are you looking at automating that webpage using perl? Or do you want to write a perl CGI script that displays the html and handles the form data?
# 3  
Old 03-26-2013
Do you think I should use html form and pl for script to get data from my flat file or i should use pl for form and also pull info too? I just need to know can perl handle search multiple fields on the same line or not. Thanks
# 4  
Old 03-26-2013
Yes, you can do all of it using perl. You need to learn how to handle form data in a perl CGI script.
# 5  
Old 03-29-2013
Do you know what was wrong with my code? It only show no results found loop and did not get the record i want?
Code:
#!/usr/bin/perl

use warnings;
use Time::Local;

print <<'HTML';
Content-type: text/html

<HTML><HEAD><TITLE>test</TITLE>
</HEAD>
<BODY TEXT="blue" LINK="blue" VLINK="#9900CC" ALINK="#330066">
<CENTER><h1><font color=black>Search Records</font></h1></CENTER>
<BR>

<center><FONT size=+1><TABLE BORDER=1><TR bgcolor=#669900><TD><center><font color=white>Animal</center></TD><TD><center><font color=white>Location</center></TD><TD><font color=white><center>Color</center></TD><TD><font color=white><center>Training</center></TD><TD><font color=white><center>Start Date</center></TD><TD><font color=white><center>End Date</center></TD></TR>

HTML
open (DATA,"./myfile.txt") || die ("Can't Open data File \n");
@data=<DATA>;
close DATA;

foreach $line (@data) {
	
	$x++;
	($ani,$loc,$colors,$training,$startdate,$enddate)=split(/\,/,$line);
	
my $startcollect = &date_to_unix_time($startdate);
my $endcollect = &date_to_unix_time($enddate);
my $startfrom = &date_to_unix($inputs{"startdate"});
my $endfrom = &date_to_unix($inputs{"enddate"});
$animal = $inputs{'animal'};
$location = $inputs{'location'};
$colour= $inputs{'color'}; 
$trainned = $inputs{'training'}; 
			if (($startcollect <= $startfrom) && ($endcollect <= $endfrom) && ($ani eq '$animal') && ($loc eq '$location') && ($colors eq '$colour') && ($training eq '$trainned'))
			{
		
			print "<TD BGCOLOR='d3d3d3'>$ani</TD><TD BGCOLOR='d3d3d3'>$loc</TD><TD BGCOLOR='d3d3d3'>$colors</TD><TD BGCOLOR='d3d3d3'>$training</TD><TD BGCOLOR='d3d3d3'>$startdate</TD><TD BGCOLOR='d3d3d3'>$enddate&nbsp;</TD></TR> \n";
						 
			} else {
			    print "No results found.<p>\n";
}
}
print <<'HTML';
</TABLE></font></CENTER>
<BR><BR><BR><BR>
    </BODY></HTML>
HTML



sub date_to_unix()
{
    my ($year,$mon,$day,$hour,$min,$sec) =
        $_[0] =~ /(\d+)-(\d+)-(\d+)/;
    return undef unless ($day and $mon and $year);
    return timelocal(0,$min,$hour,$day,$mon-1,$year-1900);
}
sub date_to_unix_time()
{
    my ($year,$mon,$day,$hour,$min,$sec) =
        $_[0] =~ /(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/;
    return undef unless ($day and $mon and $year);
    return timelocal(0,$min,$hour,$day,$mon-1,$year-1900);
}

Thanks for your help
# 6  
Old 03-29-2013
DATA

Hi,

Safer not to use DATA as it is a reserved word.

Furthermore you are reading only one line into @data ...

And this line will never eval to true because of apostrophs around $animal...
Code:
if (($startcollect <= $startfrom) && ($endcollect <= $endfrom) && ($ani eq '$animal')

And %inputs is never defined

And it is unsafe code as hell, please use %in for passed parameters ; start code with
Code:
use CGI

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl: search replace in multiple files

When I use special characters the command to replace multiple files with a string pattern does nt work. ---------- Post updated at 12:33 PM ---------- Previous update was at 11:38 AM ---------- This works perl -pi -e 's/100/test/g' * This does nt work perl -pi -e 's... (1 Reply)
Discussion started by: w020637
1 Replies

2. Shell Programming and Scripting

Search and swap multiple lines in file using Perl

Hi all, I have a vcd file with a bunch of lines containing an array, like this $var wire 1 b a $end $var wire 1 c a $end $var wire 1 d a $end $var wire 1 e a $end $var wire 1 f b $end $var wire 1 g b $end $var wire 1 h b $end $var wire 1 i b $end I want it like this: $var wire 1 e a... (12 Replies)
Discussion started by: veerabahu
12 Replies

3. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

4. Shell Programming and Scripting

Multiple pattern search in perl

user 10 values content is: musage.py yes value user 11 values content is: gusage.py yes value how to print "user" string line by searching "content is:" string and "usage.py" string in perl (8 Replies)
Discussion started by: Anjan1
8 Replies

5. UNIX for Dummies Questions & Answers

Formatting Multiple fields on 1 line to multiple rows

I'm trying extract a number of filename fields from a log file and copy them out as separate rows in a text file so i can load them into a table. I'm able to get the filenames but the all appear on one line. I tried using the cut command with the -d (delimiter) option but cant seem to make it... (1 Reply)
Discussion started by: Sinbad-66
1 Replies

6. Post Here to Contact Site Administrators and Moderators

Search fields

I routinely use the search field in the top right hand corner. When the results to a search are displayed, two new search input fields are shown at the top of the search results: google('this_site') google('the_world') Is there a reason for having 3 search input fields on the... (0 Replies)
Discussion started by: figaro
0 Replies

7. Shell Programming and Scripting

help with search and replace in multiple fields

I have a pipe delimited file with 27 fields. Each record has 26 fields. I need to search for the 25,26,27 fields and replace "," with nothing. How can I acheive this. Sed is more preferred. e.g data row o/p (5 Replies)
Discussion started by: dsravan
5 Replies

8. Shell Programming and Scripting

Perl - How to search a text file with multiple patterns?

Good day, great gurus, I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully.... (2 Replies)
Discussion started by: Sp3ck
2 Replies

9. Shell Programming and Scripting

How to search for two fields

Hi all, I have a file with format as below: cat 0 animal 90 number90 cat_number_name cat 1 animal 91 number91 cat_animal_name cat 2 animal 92 number92 cat_name_animal kiwi 0 bird 90 number90 ... (3 Replies)
Discussion started by: jisha
3 Replies

10. Shell Programming and Scripting

Perl: Match a line with multiple search patterns

Hi I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns. Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
Discussion started by: Juha
10 Replies
Login or Register to Ask a Question