如何提取文本,保存,然后将其输出到网络?

时间:2008-10-20 08:20:41

标签: perl search cgi extract

对于HF50(HF $ HF)我是searching,例如在“MyFile.txt”中,因此提取的数据必须保存为“save.txt”。现在再次提取“save.txt”上的数据,并在我的表格中填写参数和输出。但是当我尝试代码时,我没有输出,“save.txt”是空白的。?

无论我输入什么,都无法识别Var $ HF。请帮忙。

#! /usr/bin/perl

print "Content-type:text/html\r\n\r\n";

use CGI qw(:standard);
use strict;
use warnings;

my ($file,$line,$tester,$HF,$keyword);
my ($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19);

my $keyWord=param('keyword');
$HF=$keyWord;

my $infile='MyFile.txt';
my $outfile='save.txt';

open (my $inhandle, '<',$infile) or die "Can't open $infile:$!";
open (my $outhandle, '>', $outfile) or die "Can't open $outfile:$!";


while (my $line=<$inhandle>){
if ($line=~ m/HF$HF/i) {
print {$outhandle}$line;
print $line;

print "<HTML>";
print "<head>";
print "<body bgcolor='#4682B4'>";
print "<title>FUSION SHIFT REPORT</title>";
print "<div align='left'>";
print "<FORM METHOD='get'        ACTION='http://Shielex.com/pe/mrigos/mainhead.html'>";
print "<b>SEACRH:</b>";
print "<INPUT TYPE='text' NAME='rec' SIZE='12' MAXLENGHT='40'>";
print "<INPUT TYPE='submit' value='go'>";
print "</form>";
print "<TABLE CELLPADDING='1' CELLSPACING='1' BORDER='1' bordercolor=black  width='100%'>";
print "<TR>";

print "<td width='11%'bgcolor='#00ff00'><font size='2'>TESTER No.</td>";
print "<td width='10%'bgcolor='#00ff00'><font size='2'>DATE</td>";

print "<td width='11%'bgcolor='#00ff00'><font size='2'>DEVICE NAME</td>";
print "<td bgcolor='#00ff00'><font size='2'>TEST PROGRAM</td>";

print "<td width='10%'bgcolor='#00ff00'><font size='2'>SMSLOT</td>";

print "<td width='12%'bgcolor='#00ff00'><font size='2'>LOADBOARD</td>";


print "<td width='10%'bgcolor='#00ff00'><font size='2'>CATEGORY</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>ROOT CAUSE 1</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>ROOT CAUSE 2</td>";
print "</tr>";
print "<TR>";

$file='save.txt';
open(F,$file)||die("Could not open $file");
while ($line=<F>)
{
my @cells=($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19)=   split ',',$line;

print "<TD bgcolor='#ADD8E6'><font size='2'>$f2</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f3</TD>";

print "<TD bgcolor='#ADD8E6'><font size='2'>$f5</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f6</TD>";

print "<TD bgcolor='#ADD8E6'><font size='2'>$f8</TD>";

print "<TD bgcolor='#ADD8E6'><font size='2'>$f10</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f17</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f18</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f19</TD>";
print "</tr>";

}
}
}
close F;
print "</TABLE>";
print "</body>";
print "<html>";


= MyFile.txt数据 =
1,HF50,13-OCT-08,04:17:53,761503BZZGR-62,B2761503BP22.EVA,DWP,DWP,校准
2,HF60,13-OCT-08,04:17:53,761503BZZGR-62,B2761503BP22.EVA,DWP,DWP,校准
1,HF50,13-OCT-08,04:17:53,761503BZZGR-62,B2761503BP22.EVA,DWP,DWP,校准

3 个答案:

答案 0 :(得分:1)

您是将其作为CGI脚本运行吗?在这种情况下,您可能无权打开文件进行编写。您是否检查了错误日志,以查看来自die的邮件是否在那里?

您可能想查看Troubleshooting Perl CGI scripts。完成所有步骤而不跳过任何步骤。当您遇到困难时,您拥有帮助我们帮助您的大部分信息。

祝你好运,:))

答案 1 :(得分:0)

你永远不会关闭$outfile所以它不会被刷新。但是,您可能希望将数据存储在数组中吗?

顺便说一下,你应该总是使用open()的三参数形式,你也应该在使用CGI程序时总是使用绝对路径,因为在很多情况下,“当前目录”不是你的认为是。

答案 2 :(得分:-1)

首先,Perl的输出本质上是缓冲的。因此,除非您使用某种显式方法,否则无法保证物理文件可以读取任何内容。正如有人提到的,你必须以某种方式刷新输出。我的评论在下面的代码中。 (您也可以通过关闭输出文件并在读取后以追加模式打开它来执行此操作。)

其次,看起来你不想做你想做的事情。如果所有内容都完全刷新到文件,那么您需要为每个输入行请求一个html标题。因此,当我在输入中添加行时,它会打印出许多搜索框。我不指望那是你想要的。

以下是更多 perl-ified 代码:

use CGI qw(:standard);
use IO::File;
use strict;
use warnings;

my ($file,$line,$HF); #,$tester,$HF,$keyword);
# don't pollute -> my ($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10
# ,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19);


# my $keyWord=param('keyword'); <-- if you're not going to do anything with $keyWord
$HF=param('keyword'); # <- assign it to the variable you're going to use

my $infile='MyFile.txt';
my $outfile='save.txt';

open (my $inhandle, '<',$infile) or die "Can't open $infile:$!";
open (my $outhandle, '>', $outfile) or die "Can't open $outfile:$!";
# this would flush -> my $outhandle = IO::File->new( ">$outfile" );

print q{Content-type:text/html

<HTML>
<head>
<title>FUSION SHIFT REPORT</title>
<style type="text/css">
.header { background-color : #0f0; font-size : 12pt }
.detail { background-color : #ADD8E6; font-size : 12pt }
</style>
</head>
<body bgcolor='#4682B4'>
<div align='left'>
<FORM METHOD='get' ACTION='http://Shielex.com/pe/mrigos/mainhead.html'>
<b>SEACRH:</b>
<input type='text' name='rec' size='12' maxlenght='40'>
<input type='submit' value='go'>
</form>
<table cellpadding='1' cellspacing='1' border='1' bordercolor=black  width='100%'>
<tr>
  <td class="header" width='11%'>TESTER No.</td>
  <td class="header" width='10%'>DATE</td>
  <td class="header" width='11%'>DEVICE NAME</td>
  <td class="header" >TEST PROGRAM</td>
  <td class="header" width='10%'>SMSLOT</td>
  <td class="header" width='12%'>LOADBOARD</td>
  <td class="header" width='10%'>CATEGORY</td>
  <td class="header" width='13%'>ROOT CAUSE 1</td>
  <td class="header" width='13%'>ROOT CAUSE 2</td>
</tr>
}; 

my $hf_str = ",HF$HF,";
# OO -> $outhandle->autoflush(); <- set autoflush
while (my $line=<$inhandle>){
    next unless index( $line, $hf_str ) > -1;
    # OO -> $outhandle->print( $line );
    #       $outhandle->flush(); <- if autoflush not set, do it manually
    print *{$outhandle} $line;
    print "<tr>"
        , ( map { qq{<td class="detail">$_</td>} } 
            split ',', $line
           )
        , "</tr>\n"
        ;
}
print q{
</table>
</body>
</html>
};