寻找 PCRE 的替代品 [关闭]

问题描述 投票:0回答:1

Linux 的 find 命令不支持 perl 兼容的正则表达式 (PCRE)。 有没有一种替代方法可以做到这一点,使用起来很简洁(命令行上的一行)。

我尝试直接使用 perl,但没有找到适合它的单行程序。

regex linux find pcre
1个回答
0
投票

使用 Perl 的 File::Find

perl -MFile::Find -wE'find( sub { say $File::Find::name if /\.pl$/ }, q(.) )'

这会找到所有以

.pl
结尾的条目,递归地在当前目录下的任何地方。

或者以目录为输入,默认当前目录

perl -MFile::Find -wE' $d = shift//q(.); 
    find( sub { say $File::Find::name if /\.pl$/ }, $d )' directory-name

或组装找到的所有文件以进行一些可能的后处理,写入文件等

perl -MFile::Find -wE' $d = shift//q(.); 
    find( sub { push @f, $File::Find::name if /\.pl$/ }, $d ); 
    say for @f'  directory-name

(如果在没有目录名的情况下运行,则使用当前目录)

但是,我不明白为什么

find
+
grep
的简单管道不合适。
grep
本身支持基本正则表达式,而
-E
支持扩展正则表达式,而
-P
(Perl) 使用 PCRE。所以

find ... | grep -P regex 

在一个命令行中完全按照要求执行。然后可以拆分文件名的标准,一些与

find
自己的通配符一起使用,一些在
grep
的正则表达式中。

© www.soinside.com 2019 - 2024. All rights reserved.