Windows 如何在perl中的两个已知行之间搜索多行txt文件

Windows 如何在perl中的两个已知行之间搜索多行txt文件,windows,perl,Windows,Perl,我是perl新手,正在尝试用perl tk编写程序。我大部分都有,但似乎一点也记不下来 我正在读一个.txt文件,看起来像这样 display ("Blue") .... .... .... .... display ("Yellow") .... bobby .... .... display ("Red") .... .... and so on “…”是我并不真正关心的数据部分,但我必须搜索才能找到“bobby” 我已经建立了一个数组,它存储了所有显示在其中的行。i、 e.@阵列。我想在

我是perl新手,正在尝试用perl tk编写程序。我大部分都有,但似乎一点也记不下来

我正在读一个.txt文件,看起来像这样

display ("Blue")
....
....
....
....
display ("Yellow")
....
bobby
....
....
display ("Red")
....
.... and so on
“…”是我并不真正关心的数据部分,但我必须搜索才能找到“bobby”

我已经建立了一个数组,它存储了所有显示在其中的行。i、 e.@阵列。我想在原始.txt文件中的每一行之间搜索“bobby”,并输出上面的任何显示行

对于上面的示例,我希望输出

显示(“黄色”)

我想我需要一个while循环在我想要搜索的显示之间的所有区域之间迭代

下面的代码显示了我构建的一些内容。 在输入后按下按钮时,下面的子菜单将运行

#Output Substation ID to Pane
sub find_substations 
{
       #Copy the entry
my $substation_to_search = $find_ensub->get();

#Insert the copied entry into the outptutext_box
$output_textbox->insert("end", "Substation ID: $substation_to_search\n\n");

       open(myfile1, "file and file location");
       my @lines = <myfile1>;
       #Open File and Print all lines with display in it.
       my @array= grep(/display /,@lines);
       #Array Size
       my $size = @array;
       my $start = $lines[0];
       my $stop = $lines[1];

    while("$x" < "$size")
    {       
     if("$start".."$stop")
     {  
      next if /$start/||/$stop/;
      if()          
      {
        $output_textbox->insert("end", "$lines[0]");
      }#if stop
      }#If stop

     $x++;
     }
   #close myfile1;
#将变电站ID输出到窗格
变电站
{
#复制条目
我的$substation\u to\u search=$find\u ensub->get();
#将复制的条目插入输出文本框
$output\u textbox->insert(“end”,“Substation ID:$Substation\u to\u search\n\n”);
打开(myfile1,“文件和文件位置”);
我的@lines=;
#打开文件并打印其中显示的所有行。
my@array=grep(/display/,@lines);
#数组大小
我的$size=@数组;
my$start=$lines[0];
my$stop=$lines[1];
而(“$x”<“$size”)
{       
如果(“$start”。“$stop”)
{  
下一个if/$start/| |/$stop/;
if()
{
$output_textbox->insert(“end”,“$lines[0]”);
}#如果停止
}#如果停止
$x++;
}
#关闭myfile1;
}


我一直在读关于开始和停止的书,但不能让它正常工作。有人能帮我吗?“开始-停止”部分的代码不完整。

如果我正确理解了您的问题,我认为您将问题复杂化了。无需跟踪边界(您的
$start
$end
),只需跟踪
显示的当前值即可。例如:

infle:

display ("Blue")
foo
bar
display ("Yellow")
baz
bobby
qux
display ("Red")
pop
bobby
 #!/usr/bin/perl
 use strict;
 use warnings;

 open(my $fh, '<', "./".$ARGV[0]) or die("Cannot open file");

 my $display;
 while (<$fh>) {
   $display = $_ if /^display/;
   print $display if /^bobby$/;
 }
script.pl:

display ("Blue")
foo
bar
display ("Yellow")
baz
bobby
qux
display ("Red")
pop
bobby
 #!/usr/bin/perl
 use strict;
 use warnings;

 open(my $fh, '<', "./".$ARGV[0]) or die("Cannot open file");

 my $display;
 while (<$fh>) {
   $display = $_ if /^display/;
   print $display if /^bobby$/;
 }

首先,不要不必要地在变量周围加引号。当你试图把它们当作数字来对待时,这也可能会咬你:
“$x”<“$size”
谢谢!我不确定是否有命令可以执行上面的操作。完美的经过一些修改,它可以完美地工作。