variable file glob into grep without glob()

Q

qanda

Hope I don't get shot for posting again, but I really need help with
this ...

I want to process lots (thousands) of files. I want to take a file
glob from the command line and pass it into a grep. If I could write
the file glob (file*.ext for example) directly I would do ...

local @ARGV = grep /file.*\.ext/, readir CURDIR;

However I need to take the glob from the command line. I can't use
the Perl glob as I am restricted to Perl 5.005 which dosen't work with
large argument strings and the only overhead I want for the user is to
possibly quote the glob, maybe myscript "file*.ext"; I do not want
them using find and/or xargs.

Thanks.
 
T

Tassilo v. Parseval

Also sprach qanda:
Hope I don't get shot for posting again, but I really need help with
this ...

I want to process lots (thousands) of files. I want to take a file
glob from the command line and pass it into a grep. If I could write
the file glob (file*.ext for example) directly I would do ...

local @ARGV = grep /file.*\.ext/, readir CURDIR;

However I need to take the glob from the command line. I can't use
the Perl glob as I am restricted to Perl 5.005 which dosen't work with
large argument strings and the only overhead I want for the user is to
possibly quote the glob, maybe myscript "file*.ext"; I do not want
them using find and/or xargs.

In this case translate the glob pattern into a regular expression. You
don't have to do that yourself but let Text::Glob from the CPAN do this
work for you:

use Text::Glob qw(glob_to_regex);
...
my $pat = glob_to_regex(shift);
local @ARGV = grep /$pat/, readir CURDIR;

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top