我怎样才能模拟“grep -B”? Perl 中的功能?

发布于 2024-12-17 23:22:01 字数 428 浏览 0 评论 0原文

我一直在寻找一种解决方案,允许我搜索数组的行,如果字符串匹配,则将该行和前两行推入数组中。使用 grep 命令似乎可以轻松完成此操作。但是,我无法让它发挥作用。这就是我所拥有的:

sub ipsearch {
    my $ip = $_[0];
    my @IPVSCONFIG =  grep (/\W+virtual\s$ip\s/, @RAWDATA);
}

是否可以将“-B 2”语法添加到 grep 命令中?我已经尝试了这种语法的几种形式,但我无法让它工作:

@IPVSCONFIG =  grep -B 2 (/\W+virtual\s$ip\s/, @RAWDATA);

请让我知道这是否可能,以及正确的语法应该是什么。如果您有任何其他建议,请告诉我。

感谢您的帮助!

I have been searching for a solution that allows me to search the lines of an array, and if a string match is made, push that line and the 2 previous lines into an array. It seems this would be easily done using the grep command. However, I cannot get this to work. This is what I have:

sub ipsearch {
    my $ip = $_[0];
    my @IPVSCONFIG =  grep (/\W+virtual\s$ip\s/, @RAWDATA);
}

Is it possible to add the "-B 2" syntax to the grep command? I've tried several forms of this syntax but I cannot get it to work:

@IPVSCONFIG =  grep -B 2 (/\W+virtual\s$ip\s/, @RAWDATA);

Please let me know if this is even possible, and what the correct syntax should be. If you have any other suggestions, please let me know.

Thanks for the help!

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

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

发布评论

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

评论(3

勿忘心安 2024-12-24 23:22:01

诀窍是识别发生匹配的行,然后识别周围的相关索引:

获取匹配的索引:

my @matchedIndices = grep { $RAWDATA[$_] =~ /\W+virtual\s$ip\s/ } 2 .. $#RAWDATA;

获取周围的索引:

my @wantedIndices  = map { ( $_-2 .. $_ ) } @matchedIndices;

并获取一个数组切片:

my @IPVSCONFIG = @RAWDATA[ @wantedIndices ];

将其完全放入 Schwartzian 变换中:

my @IPVCONFIG = map  { @RAWDATA[$_-2..$_] }
                grep { $RAWDATA[$_] =~ /\W+virtual\s$ip\s/ }
                2 .. $#RAWDATA ;

绝对是一个比传统命令行grep -B 2

The trick is to identify the lines where the match occurs, then identify the relevant indices around:

Get the matched indices:

my @matchedIndices = grep { $RAWDATA[$_] =~ /\W+virtual\s$ip\s/ } 2 .. $#RAWDATA;

Get the indices around:

my @wantedIndices  = map { ( $_-2 .. $_ ) } @matchedIndices;

And take an array slice:

my @IPVSCONFIG = @RAWDATA[ @wantedIndices ];

Putting it altogether in a Schwartzian transform:

my @IPVCONFIG = map  { @RAWDATA[$_-2..$_] }
                grep { $RAWDATA[$_] =~ /\W+virtual\s$ip\s/ }
                2 .. $#RAWDATA ;

Definitely a much busier solution than the traditional command-line grep -B 2!

不及他 2024-12-24 23:22:01

子例程的基本版本。我假设您想在完成后返回该列表。未经测试。

sub ipsearch {
    my $ip = shift;
    my @IPVSCONFIG = (); # no matches should be empty list, not undef
    my @buffer = ()      # to avoid undef warnings
    for (@RAWDATA) {
        push @buffer, $_;
        shift @buffer if @buffer > 3;
        if (/\W+virtual\s$ip\s/) {
            push @IPVSCONFIG, @buffer;
            @buffer = ();
        }
    }
    return @IPVSCONFIG;
}

A basic version of your subroutine. I assume you wanted to return the list when done with it. Untested.

sub ipsearch {
    my $ip = shift;
    my @IPVSCONFIG = (); # no matches should be empty list, not undef
    my @buffer = ()      # to avoid undef warnings
    for (@RAWDATA) {
        push @buffer, $_;
        shift @buffer if @buffer > 3;
        if (/\W+virtual\s$ip\s/) {
            push @IPVSCONFIG, @buffer;
            @buffer = ();
        }
    }
    return @IPVSCONFIG;
}
疯狂的代价 2024-12-24 23:22:01

您将 grep 程序 /bin/grep 与名为 grep (perldoc -f grep) 的 Perl 函数混淆了。虽然前者需要额外的参数,如 -B,但后者则不需要。

You are mixing up the grep program /bin/grep with the perl function named grep (perldoc -f grep). While the former takes additional parameters, like -B, the latter does not.

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