perl-子字符关键字

发布于 2025-01-29 13:46:43 字数 597 浏览 1 评论 0原文

我有一个文本文件,其中有很多行,我需要在此文件关键字中进行搜索,如果存在写入日志文件行,则关键字是关键字,并在下面的一行和一条线上行。现在,如果查找全部写入,我不知道如何在下面和更高版本上编写行,现在搜索或写关键字不函数。感谢您的建议。

my $vstup = "C:/Users/Omega/Documents/Kontroly/testkontroly/kontroly20220513_154743.txt";
my $log = "C:/Users/Omega/Documents/Kontroly/testkontroly/kontroly.log";
    
open( my $default_fh, "<", $vstup ) or die $!;
open( my $main_fh,    ">", $log )    or die $!;

my $var = 0;
while ( <$default_fh> ) { 
    if (/\Volat\b/)
        $var = 1;
    }
    if ( $var )
        print $main_fh $_;
    }
}

close $default_fh;
close $main_fh;

I have a text file where is lot of lines, I need search in this file keywords and if exist write to log file line where is keywords and line one line below and one above the keyword. Now search or write keyword not function if find write all and I dont known how can I write line below and above. Thanks for some advice.

my $vstup = "C:/Users/Omega/Documents/Kontroly/testkontroly/kontroly20220513_154743.txt";
my $log = "C:/Users/Omega/Documents/Kontroly/testkontroly/kontroly.log";
    
open( my $default_fh, "<", $vstup ) or die $!;
open( my $main_fh,    ">", $log )    or die $!;

my $var = 0;
while ( <$default_fh> ) { 
    if (/\Volat\b/)
        $var = 1;
    }
    if ( $var )
        print $main_fh $_;
    }
}

close $default_fh;
close $main_fh;

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

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

发布评论

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

评论(1

真心难拥有 2025-02-05 13:46:43

下面的方法使用一个信号量变量和一个缓冲区变量来提供所需的行为。

请注意,用于简单测试的A替换了所使用的图案。

#!/usr/bin/perl

use strict;
use warnings;


my ($in_fh, $out_fh);
my ($in, $out);
$in = 'input.txt';
$out = 'output.txt';

open($in_fh, "< ", $in) || die $!."\n";
open($out_fh, "> ", $out) || die $!;

my $p_next = 0;
my $p_line;
while (my $line = <$in_fh>) {
  # print line after occurrence
  print $out_fh $line if ($p_next);

  if ($line =~ /A/) {
    if (defined($p_line)) {
      # print previous line
      print $out_fh $p_line;

      # once printed undefine variable to avoid printing it again in the next loop
      undef($p_line);
    }
    
    # Print current line if not already printed as the line follows a pattern
    print $out_fh $line if (!$p_next);
    
    # toggle semaphore to print the next line
    $p_next = 1;

  } else {
    # pattern not found.
    
    # if pattern was not detected in both current and previous line.
    $p_line = $line if (!$p_next);
    $p_next = 0;
  }
}
close($in_fh);
close($out_fh);

The approach below uses one semaphore variable and a buffer variable to provide the desired behavior.

Notice that the pattern used was replaced by A` for simplicity testing.

#!/usr/bin/perl

use strict;
use warnings;


my ($in_fh, $out_fh);
my ($in, $out);
$in = 'input.txt';
$out = 'output.txt';

open($in_fh, "< ", $in) || die $!."\n";
open($out_fh, "> ", $out) || die $!;

my $p_next = 0;
my $p_line;
while (my $line = <$in_fh>) {
  # print line after occurrence
  print $out_fh $line if ($p_next);

  if ($line =~ /A/) {
    if (defined($p_line)) {
      # print previous line
      print $out_fh $p_line;

      # once printed undefine variable to avoid printing it again in the next loop
      undef($p_line);
    }
    
    # Print current line if not already printed as the line follows a pattern
    print $out_fh $line if (!$p_next);
    
    # toggle semaphore to print the next line
    $p_next = 1;

  } else {
    # pattern not found.
    
    # if pattern was not detected in both current and previous line.
    $p_line = $line if (!$p_next);
    $p_next = 0;
  }
}
close($in_fh);
close($out_fh);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文