我的Perl脚本不工作,我有一种感觉,这是grep命令 [英] My perl script isn't working, I have a feeling it's the grep command
问题描述
我试图在一个文件中搜索
数字的实例,如果其他文件包含这些数字,则发布。
#!/ usr / bin / perl
open(file,textIds.txt); #
@file =< file>; #file寻找
#关闭文件; #
while(<>){
$ temp = $ _;
$ temp =〜tr / | / \ t /;名称和ID之间的#puts选项卡
@arrayTemp = split(\t,$ temp);
@ found = grep {/ $ arrayTemp [1] /}< file>;
if(defined $ found [0]){
#if(grep {/ $ arrayTemp [1] /}< file>){
print $ _;
}
@ found =();
}
打印\\\
;
关闭文件;
#输入文件行的格式为
#John | 7791 154
#Smith | 5432 290
#Conor | 6590 897
#此文件格式为
#5432
#7791
#6590
#23140
脚本中存在一些问题。 c $ c>和使用警告;
。
这将在您的脚本中事先告诉您有关奇怪的事情。
来代替: open(my $ fh,'&','testIds.txt');
使用autodie;
或检查打开的结果是否有效。您阅读并存储 testIds。 txt
放入数组 @file
但稍后(在 grep
中),您是
再次尝试从该文件(句柄)中读取(使用< file>
)。正如@PaulL所说,由于该文件已被读取,因此总是
给出 undef
(false)。
|
带有制表符,然后在选项卡上拆分并不是必需的。您可以同时在
标签和管道上分割(假设John | 7791 154真的是John | 7791 \ t154)。
我假设你的textIds.txt是只有数字的文本,而其他的输入文件是从STDIN读取的
(带有
考虑到这一点,您的脚本可以写成: p>
#!/ usr / bin / perl
use strict;
使用警告;
#打开'textIds.txt'并将其放入数组@file:
open(my $ fh,'<','textIds.txt')或者死无法打开file:$!\\\
;
my @file =< $ fh>;
close($ fh);
#迭代STDIN并与'textIds.txt'中的行进行比较:
while(my $ line =<>){
#splitJohn | 7791 (John,7791,154):
my($ name,$ number1,$ number2)= split(/ \ || \ t /,$ line);
#将$ number1与@file的每个成员进行比较,如果找到则打印:
if(grep(/ $ number1 /,@file)){
print $ line;
}
}
I'm trying for search in the one file for instances of the number and post if the other file contains those numbers
#!/usr/bin/perl
open(file, "textIds.txt"); #
@file = <file>; #file looking into
# close file; #
while(<>){
$temp = $_;
$temp =~ tr/|/\t/; #puts tab between name and id
@arrayTemp = split("\t", $temp);
@found=grep{/$arrayTemp[1]/} <file>;
if (defined $found[0]){
#if (grep{/$arrayTemp[1]/} <file>){
print $_;
}
@found=();
}
print "\n";
close file;
#the input file lines have the format of
#John|7791 154
#Smith|5432 290
#Conor|6590 897
#And in the file the format is
#5432
#7791
#6590
#23140
There are some issues in your script.
- Always include
use strict;
anduse warnings;
. This would have told you about odd things in your script in advance. - Never use barewords as filehandles as they are global identifiers. Use three-parameter-open
instead:
open( my $fh, '<', 'testIds.txt');
use autodie;
or check whether the opening worked.- You read and store
testIds.txt
into the array@file
but later on (in yourgrep
) you are again trying to read from that file(handle) (with<file>
). As @PaulL said, this will always giveundef
(false) because the file was already read. - Replacing
|
with tabs and then splitting at tabs is not neccessary. You can split at the tabs and pipes at the same time as well (assuming "John|7791 154" is really "John|7791\t154"). - Your talking about "input file" and "in file" without exactly telling which is which.
I assume your "textIds.txt" is the one with only the numbers and the other input file is the
one read from STDIN (with the
|
's in it).
With this in mind your script could be written as:
#!/usr/bin/perl
use strict;
use warnings;
# Open 'textIds.txt' and slurp it into the array @file:
open( my $fh, '<', 'textIds.txt') or die "cannot open file: $!\n";
my @file = <$fh>;
close($fh);
# iterate over STDIN and compare with lines from 'textIds.txt':
while( my $line = <>) {
# split "John|7791\t154" into ("John", "7791", "154"):
my ($name, $number1, $number2) = split(/\||\t/, $line);
# compare $number1 to each member of @file and print if found:
if ( grep( /$number1/, @file) ) {
print $line;
}
}
这篇关于我的Perl脚本不工作,我有一种感觉,这是grep命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!