使用perl在文本文件中创建grep word
我的文件夹中有一个文本文件A00010.txt A00011.txt A00012.txt到A00099.txt,其中包含不同的条目,如使用perl在文本文件中创建grep word,perl,command-line,Perl,Command Line,我的文件夹中有一个文本文件A00010.txt A00011.txt A00012.txt到A00099.txt,其中包含不同的条目,如 umxwtdn8vtnt_n umxwtdtnt_nn8v umxwt_ntdn8vtn u8vtnt_nmxwtdn utnt_nmxwtdn8v 我的perl代码是 #!/usr/bin/perl use strict; my $count = 10; for ($count = 10; $count<= 99;
umxwtdn8vtnt_n
umxwtdtnt_nn8v
umxwt_ntdn8vtn
u8vtnt_nmxwtdn
utnt_nmxwtdn8v
我的perl代码是
#!/usr/bin/perl
use strict;
my $count = 10;
for ($count = 10; $count<= 99; $count++) {
my $result = `/bin/cat /myfolder/A000$count.txt | grep "umxwtdn8vtnt_n"`;
return $result;
}
print $result;
#/usr/bin/perl
严格使用;
我的$count=10;
对于($count=10;$count您真的需要使用Perl吗
如果没有:
find /myfolder -name "A000??.txt" | xargs grep -n "umxwtdn8vtnt_n"
将在您的文件中找到模式并告诉您在哪一行
您想知道该模式是否存在于一个或多个文件中吗?然后:
my $not_found = 1;
for (my $count = 10; $count<= 99; $count++) {
my $result = `grep "umxwtdn8vtnt_n" /myfolder/A000$count.txt`;
if ($result) {
print $result;
$not_found = 0; # error level 0 = no error = found
last;
}
}
exit $not_found; # error level 1 = error = not found
my$not\u found=1;
对于(my$count=10;$count您真的需要使用Perl吗
如果没有:
find /myfolder -name "A000??.txt" | xargs grep -n "umxwtdn8vtnt_n"
将在您的文件中找到模式并告诉您在哪一行
您想知道该模式是否存在于一个或多个文件中吗?然后:
my $not_found = 1;
for (my $count = 10; $count<= 99; $count++) {
my $result = `grep "umxwtdn8vtnt_n" /myfolder/A000$count.txt`;
if ($result) {
print $result;
$not_found = 0; # error level 0 = no error = found
last;
}
}
exit $not_found; # error level 1 = error = not found
my$not\u found=1;
因为(我的$count=10;$count是/myfolder
真的在根目录中吗?(你在ls/
中看到了什么?你看到了myfolder
?)在Unix系统中,在根目录中添加东西是非常罕见的,而且我认为你不会弄乱/
另外,您正在子例程(sub{}
)之外返回$result
),如果是这种情况,您应该会得到一个Perl运行时错误
如果您正在复制代码片段,请注意,$result
是一个局部变量,它在子例程结束后消失。是否/myfolder
确实在根目录中?(您在ls/
中看到了什么?是否看到myfolder
?)在Unix系统中,在根目录中添加东西是非常罕见的,而且我认为您没有弄乱/
另外,您正在子例程(sub{}
)之外返回$result
),如果是这种情况,您应该会得到一个Perl运行时错误
如果您正在复制代码片段,请注意,$result
是一个局部变量,它在子例程结束后消失。您试图获取$result值是什么意思?您想要所有匹配的行吗?不,我只需要一个值。然后我可以告诉您答案:您要查找的值是umxwtdn8vtnt_n
。这没有意义。print show empty但如果我在turminal上运行命令,其show output仍在尝试理解您的问题,对不起。您是说您想知道模式是否至少在您的一个文件中?您试图获取$result值是什么意思?您想要所有匹配行吗?不,我只需要一个值onlyThen我可以告诉你答案:你要找的值是umxwtdn8vtnt\u n
。这没有意义。print show empty但如果我在turminal上运行命令,它的show output仍在试图理解你的问题,对不起。你是说你想知道模式是否至少在你的一个文件中?如何在f之外使用$result或者(my$count=10;$count我是指for循环外部的一个函数,我需要在这个函数中使用$result值。当您在循环的本地范围中定义时,如何在函数外部使用它?i test findmypattern function findmypattern();但不显示任何结果,如Shinkiro所说,您确定您的文件位于/myfolder
中吗?该文件夹位于根目录中的事实非常可疑。如何在外部使用$result(my$count=10;$count我指的是for循环外部的一个函数,我需要在该函数中使用$result值。当您在循环的局部范围中定义时,如何在函数外部使用它?我测试findmypattern函数findmypattern();但不显示任何结果,如Shinkiro所说,你确定你的文件在/myfolder
中吗?该文件夹在根目录中的事实非常可疑。