删除perl中包含重复正则表达式的行

发布于 2025-01-08 04:47:10 字数 509 浏览 0 评论 0原文

我有一个包含以下元素的数组:

@array = qw/ john jim rocky hosanna/;

INPUT FILE:

john wears blue shirt 

hosanna knows drawing

george and jim went to europe

john went to swimming

jim wears yellow shirt

rocky went to swimming

rocky learns painting

hosanna learns painting

REQUIRED OUTPUT:

john wears blue shirt 

hosanna knows drawing

george and jim went to europe

rocky went to swimming

所以,我只需要第一次出现的行。

I have an array that contains elements like:

@array = qw/ john jim rocky hosanna/;

INPUT FILE:

john wears blue shirt 

hosanna knows drawing

george and jim went to europe

john went to swimming

jim wears yellow shirt

rocky went to swimming

rocky learns painting

hosanna learns painting

REQUIRED OUTPUT:

john wears blue shirt 

hosanna knows drawing

george and jim went to europe

rocky went to swimming

so, I need to have only first occurrences lines.

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

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

发布评论

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

评论(4

烦人精 2025-01-15 04:47:10
@seen{@array} = ();
@out = grep { (($w)=split; !($seen{$w}++) } @in;
@seen{@array} = ();
@out = grep { (($w)=split; !($seen{$w}++) } @in;
大姐,你呐 2025-01-15 04:47:10
perl -ane 'print unless $a{$F[0]}++ ' inputfile

希望这有效+

perl -ane 'print unless $a{$F[0]}++ ' inputfile

hope this works +

潜移默化 2025-01-15 04:47:10

制作另一个数组来指示该名称是否已被使用怎么样?然后,当您第一次阅读 Jim 的行时,请将此数组中的变量设置为所使用的并写入输出中。如果过去已经使用过,则无需执行任何操作。

@array =(john,jim,rocky,hosanna);
@used =(0,0,0,0);

What about making another array which indicates if the name was already used? Then, first time you read line with Jim, set variable in this array as used and write in into output. If it was already used in the past, do nothing.

@array =(john,jim,rocky,hosanna);
@used =(0,0,0,0);
Spring初心 2025-01-15 04:47:10

单程。我将数组数据保存到哈希中,并在输入文件中找到条目时删除该条目。

script.pl 的内容:

use warnings;
use strict;

## Input names to search.
my @array = qw/ john jim rocky hosanna/;

## Save names to a hash. This way they are easier to find out.
my %names = map { $_ => 1 } @array;

## Read file line by line.
while ( <> ) { 

    ## Avoid blank lines.
    next if m/\A\s*\Z/;

    ## Split line in fields.
    my @f = split;

    ## Count number of names in hash.
    my $num_entries = scalar keys %names;

    ## Remove words of hash found in line.
    for ( @f ) { 
        delete $names{ $_ };
    }   

    ## If now there are less names, it means that line had any of
    ## them, so print line.
    if ( scalar keys %names < $num_entries ) { 
        printf qq[%s\n], $_; 
    }   

    ## If hash is empty, there are no lines left to print, so exit of
    ## loop without checking more lines.
    last if scalar keys %names == 0;
}

命令:

perl script.pl infile

输出:

john wears blue shirt 

hosanna knows drawing

george and jim went to europe

rocky went to swimming

One way. I save array data to a hash and delete an entry when found in the input file.

Content of script.pl:

use warnings;
use strict;

## Input names to search.
my @array = qw/ john jim rocky hosanna/;

## Save names to a hash. This way they are easier to find out.
my %names = map { $_ => 1 } @array;

## Read file line by line.
while ( <> ) { 

    ## Avoid blank lines.
    next if m/\A\s*\Z/;

    ## Split line in fields.
    my @f = split;

    ## Count number of names in hash.
    my $num_entries = scalar keys %names;

    ## Remove words of hash found in line.
    for ( @f ) { 
        delete $names{ $_ };
    }   

    ## If now there are less names, it means that line had any of
    ## them, so print line.
    if ( scalar keys %names < $num_entries ) { 
        printf qq[%s\n], $_; 
    }   

    ## If hash is empty, there are no lines left to print, so exit of
    ## loop without checking more lines.
    last if scalar keys %names == 0;
}

Command:

perl script.pl infile

Output:

john wears blue shirt 

hosanna knows drawing

george and jim went to europe

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