简体   繁体   English

在Perl中使用grep查找文件

[英]Using grep with perl to find files

I am new to perl, and I am currently stuck on this problem. 我是perl的新手,我目前仍在解决此问题。

  1. I have multiple files in a directory 我的目录中有多个文件
  2. I want to check their names, and see if the filename matches a certain keyword (not whole filename). 我想检查他们的名字,看看文件名是否匹配某个关键字(不是整个文件名)。 So in conclusion, I want to grab the certain files that all have a certain keyword, then process them. 因此,总而言之,我想获取所有具有特定关键字的特定文件,然后对其进行处理。

I was trying something like 我在尝试类似

grep -rl "keyword" /.; 
#where does the filenames get stored? let's say in $_?
#foreach valid file, do something

from some website I found, but it doesn't seem to work? 从我发现的某个网站上获取,但似乎不起作用? Help please, Thanks!! 请帮助,谢谢!

How about 怎么样

 ls *keyword*

If you trying to do this within perl 如果您尝试在perl中执行此操作

 @files = glob("*keyword*");
 for $file (@files)
 {
   print "$file\n";
 }

Note that grep in perl is a core function, but it has nothing to do with regular expressions. 请注意,perl中的grep是核心功能,但与正则表达式无关。 It is a more like SQL where ; 它更像SQL的where it filters an array to a subarray by applying a function (which may or may not be a regex) to each element. 它通过对每个元素应用函数(可能是也可能不是正则表达式)来将数组过滤为子数组。

If glob expressions are not good enough, you can do 如果glob表达式不够好,您可以执行

 @files = grep /(fun[kK]y_)keyword?/ glob("*");
perl -E 'say for <*keyword*>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM