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

发布于 2024-07-06 18:49:49 字数 3557 浏览 2 评论 0原文

我正在搜索 HF50(HF$HF) 例如在“MyFile.txt”中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,校准

I am searching for HF50(HF$HF) for example in "MyFile.txt" so that the extracted data must save to "save.txt". The data on "save.txt" now extracted again and fill the parameters and output on my table. But when I tried the code, I've got no output and "save.txt" is blank.?

Var $HF is not recognized whatever I type. Please help.

#! /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 data=
1,HF50,13-OCT-08,04:17:53,761503BZZGR-62,B2761503BP22.EVA,DWP,DWP,Calibration
2,HF60,13-OCT-08,04:17:53,761503BZZGR-62,B2761503BP22.EVA,DWP,DWP,Calibration
1,HF50,13-OCT-08,04:17:53,761503BZZGR-62,B2761503BP22.EVA,DWP,DWP,Calibration

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幻想少年梦 2024-07-13 18:49:49

您是否将其作为 CGI 脚本运行? 在这种情况下,您可能没有打开文件进行写入的权限。 您是否检查了错误日志以查看来自 die 的消息是否在其中?

您可能想查看 Perl CGI 脚本故障排除。 完成所有步骤,不要跳过任何步骤。 当您遇到困难时,您可以获得帮助我们帮助您所需的大部分信息。

祝你好运, :)

Are you running this as a CGI script? In that case, you probably don't have permission to open a file for writing. Did you check the error log to see if your message from die is in there?

You might want to check out Troubleshooting Perl CGI scripts. Go through all of the steps without skipping any. When you get stuck, you have most of the imformation you need to help us help you.

Good luck, :)

許願樹丅啲祈禱 2024-07-13 18:49:49

您永远不会关闭 $outfile 因此它不会被刷新。 但也许您想将数据存储在数组中?

顺便说一句,您应该始终使用 open() 的三参数形式,并且在使用 CGI 程序时还应该始终使用绝对路径,因为在许多情况下,“当前目录”不是你认为它是什么。

You never close $outfile so it doesn't get flushed. But maybe you want to store the data in an array instead?

As an aside, you should always use the three-argument form of open() and you should also always use absolute paths when working with CGI programs, as in many situations, the "current directory" is not what you think it is.

东北女汉子 2024-07-13 18:49:49

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

其次,您似乎不想做看起来像您那样的事情想做。 如果所有内容都完美地刷新到文件中,则您将请求每个输入行一个 html 标头。 因此,当我在输入中添加行时,它会打印出许多搜索框。 我不认为这是你想要的。

下面是更perl 化的代码:

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>
};

First, Perl's output is by nature buffered. So, unless you use some explicit method, there's no guarantee that the physical file will have anything to read. As somebody mentioned, you'll have to flush the output somehow. My comments are below in the code. (You could also do this by closing the output file and opening it in append mode after you've read from it.)

Second, it doesn't seem like you want to do what it looks like you want to do. If everything was flushed perfectly to the file, you're requesting an html header per input line. So as I added lines into the input, it printed out that many search boxes. I don't expect that is what you wanted.

Here's a more perl-ified code:

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>
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文