Perl脚本grep [英] Perl script grep

查看:147
本文介绍了Perl脚本grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

脚本正在打印输入行的数量,我希望它能打印出现在另一个文件中的输入行的数量。

 #!/ usr / bin / perl -w 
open(file,text.txt);
@todd =< file>;
关闭文件; $(<>){
if(grep(/ ^ $ _ $ /,@todd)){
#if(grep @todd,/ ^ $ _ $ /) {
print $ _;
}
打印\\\
;
}

如果文件包含

  1 
3
4
5
7

以及将从中读取的输入文件

  1 
2
3
4
5
6
7
8
9

我希望它打印1,3,4,5和7
,但是正在打印1-9



UPDATE ******
这是我的代码现在,我得到这个错误
readline()在关闭文件句柄todd在./may6test.pl第3行。

 #!/ usr / bin / perl -w 
open(todd,< text。文本);
@files =< todd>; #文件正在查看
关闭todd;
while(my $ line =<>){
chomp $ line;
if(grep(/ ^ $ line $ /,@files)){
print $ _;
}
打印\\\
;
}

这对我来说没有任何意义,因为我有这个基本上正在做的其他脚本同样的事情

$ p $ #!/ usr / bin / perl -w
open(file,< text2.txt); #
@file =< file>; #文件正在查看
关闭文件; #
while(<>){
$ temp = $ _;
$ temp =〜tr / | / \ t /;名称和ID之间的#puts选项卡
my($ name,$ number1,$ number2)= split(\t,$ temp);
if(grep(/ ^ $ number1 $ /,@file)){
print $ _;
}
}
打印\\\
;


解决方案

好的,这里的问题是 - grep 也设置 $ _ 。因此, grep {$ _} @array 将始终为您提供数组中的每个元素。

在基本层面上 - 您需要:

  while( my $ line =<>){
chomp $ line;
if(grep {/ ^ $ line $ /} @todd){
#do something
}
}

但是我会建议您改为考虑构建一行代码行:

  open(my $ input,'<',text.txt)或者$! 
my%in_todd = map {$ _ => 1}< $ input>;
close $ input;
while(<>){
print in if_todd {$ _};
}

注意 - 您可能需要注意拖尾换行。

The script is printing the amount of input lines, I want it to print the amount of input lines that are present in another file

#!/usr/bin/perl -w
open("file", "text.txt"); 
        @todd = <file>;         
        close "file";
while(<>){
        if( grep( /^$_$/, @todd)){
        #if( grep @todd, /^$_$/){
                print $_;
        }
        print "\n";
}

if for example file contains

1
3
4
5
7

and the input file that will be read from contains

1
2
3
4
5
6
7
8
9

I would want it to print 1,3,4,5 and 7 but 1-9 are being printed instead

UPDATE****** This is my code now and I am getting this error readline() on closed filehandle todd at ./may6test.pl line 3.

#!/usr/bin/perl -w
open("todd", "<text.txt");
        @files = <todd>;         #file looking into
        close "todd";
while( my $line = <> ){
        chomp $line;
        if ( grep( /^$line$/, @files) ) {
                print $_;
        }
        print "\n";
}

which makes no sense to me because I have this other script that is basically doing the same thing

#!/usr/bin/perl -w
open("file", "<text2.txt");    #
        @file = <file>;         #file looking into
        close "file";           #
while(<>){
        $temp = $_;
        $temp =~ tr/|/\t/;      #puts tab between name and id
        my ($name, $number1, $number2) = split("\t", $temp);
        if ( grep( /^$number1$/, @file) ) {
                print $_;
        }
}
print "\n";

解决方案

OK, the problem here is - grep sets $_ too. So grep { $_ } @array will always give you every element in the array.

At a basic level - you need to:

while ( my $line = <> ) { 
   chomp $line; 
   if ( grep { /^$line$/ } @todd ) { 
      #do something
   }
}

But I'd suggest instead that you might want to consider building a hash of your lines instead:

open( my $input, '<', "text.txt" ) or die $!;
my %in_todd = map { $_ => 1 } <$input>;
close $input;
while (<>) {
   print if $in_todd{$_};
}

Note - you might want to watch for trailing linefeeds.

这篇关于Perl脚本grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆