简体   繁体   中英

Using grep with perl to find files

I am new to perl, and I am currently stuck on this problem.

  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

 @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. It is a more like 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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