ps_chou
ps_chou

Reputation: 79

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!!

Upvotes: 0

Views: 1908

Answers (2)

mpapec
mpapec

Reputation: 50637

perl -E 'say for <*keyword*>'

Upvotes: 1

Mark Lakata
Mark Lakata

Reputation: 20838

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("*");

Upvotes: 2

Related Questions