Reputation: 21674
Seems <>
operator cannot handle file name patterns, like file*.txt. Is there any solution for such need? For example, replace a pattern in files in some name pattern (perl -i.bak -pe s/p1/p2/g file*.txt
). I am using Windows and cmd.exe also doesn't expand the file name pattern.
Upvotes: 1
Views: 508
Reputation: 21674
Based on answer from @choroba, I think using BEGING block to translate the wildchars also works well.
perl -i.bak -pe"BEGIN{@ARGV=glob shift}s/p1/p2/g" file*.txt
or below works for multiple input patterns:
perl -i.bak -pe"BEGIN{@ARGV=map glob,@ARGV}s/p1/p2/g" file*.txt
Upvotes: 0
Reputation: 3601
The glob
operator is used to expand wildcards in file names; see perldoc -f glob. However, the built-in one considers whitespace as separators, meaning it won't handle spaces in file names correctly. Replace it with the :bsd_glob
from File::Glob. File::Glob
is a standard module and comes installed with Perl. For a list for standard modules, see perldoc perlmodlib.
use File::Glob qw( :bsd_glob );
@ARGV = map { glob } @ARGV;
This is how you can incorporate it in your one-liner:
perl -i.bak -MFile::Glob=:bsd_glob -pe"BEGIN { @ARGV = map glob, @ARGV } s/p1/p2/g" *.txt
Upvotes: 4
Reputation: 242133
In *nix, the shell is responsible for expanding wildcards in paths. In MSWin, it's upon the application to do it.
perl -i~ -we "@ARGV = glob shift; s/p1/p2/g, print while <>" file*.txt
Upvotes: 4