简体   繁体   English

如何递归地更改名称与Perl中的字符串匹配的目录中的文件?

[英]How to recursively change files in directories whose name matches a string in Perl?

I have many directories for different projects. 我有许多不同项目的目录。 Under some project directories, there are subdirectories named "matlab_programs". 在某些项目目录下,有一些名为“ matlab_programs”的子目录。 In only subdirectories named matlab_programs, I would like to replace the string 'red' with 'blue' in files ending with *.m. 在仅名为matlab_programs的子目录中,我想在以* .m结尾的文件中将字符串“ red”替换为“ blue”。

The following perl code will recursively replace the strings in all *.m files, regardless of what subdirectories the files are in. 以下perl代码将以递归方式替换所有* .m文件中的字符串,而不管文件位于哪个子目录中。

find . -name "*.m" | xargs perl -p -i -e "s/red/blue/g"

And to find the full paths of all directories called matlab_programs, 要找到所有名为matlab_programs的目录的完整路径,

find . -type d -name "matlab_programs"

How can I combine these so I only replace strings if the files are in a subdirectory called matlab_programs? 如何合并这些内容,以便仅在文件位于名为matlab_programs的子目录中时替换字符串?

Perl has the excellent File::Find module, that lets you specify a callback to be called on each file. Perl具有出色的File::Find模块,该模块可让您指定要在每个文件上调用的回调。

So you can specified a complex compound criteria, like this: 因此,您可以指定一个复杂的复合条件,如下所示:

#!/usr/bin/env perl

use strict;
use warnings;

use File::Find;

sub find_files {
    next unless m/\.m\z/;    # skip any files that don't end in .m
    if ( $File::Find::dir =~ m/matlab_programs$/ ) {
        print $File::Find::name, " found\n";
    }
}

find( \&find_files, "." );

And then you can do whatever you wish with the files you find - like opening/text replacing and closing. 然后,您可以对找到的文件进行任何操作,例如打开/替换和关闭文本。

Do you have bash ? 你有bash吗? The $(...) syntax works like backticks (the way both the shell and Perl use them) but they can be nested. $(...)语法的作用类似于反引号(shell和Perl都使用反引号的方式),但是它们可以嵌套。

perl -pi -e s/red/blue/g $(find $(find . -type d -name matlab_programs) -type f -name \*.m)

Many flavors of find also support a -path pattern test, so you can just combine your filename conditions into that argument 许多类型的find也支持-path pattern测试,因此您可以将文件名条件组合到该参数中

perl -pi -e s/red/blue/g $(find . -type f -path \*/matlab_programs/\*.m)

You want to find all directories named matlab_programs using 您想使用以下命令找到所有名为matlab_programs目录

find . -type d -name "matlab_programs"

and then execute 然后执行

find $f -name "*.m" | xargs perl -p -i -e "s/red/blue/g"

on all results $f . 在所有结果$f Judging by your use of xargs , there are no special characters such as spaces in your file names. 通过使用xargs判断,文件名中没有特殊字符,例如空格。 so the following should work: 所以以下应该工作:

find `find . -type d -name "matlab_programs"` -name "*.m" |
xargs perl -p -i -e "s/red/blue/g"

or 要么

find . -type d -name "matlab_programs" |
while read f
do
  find $f -name "*.m" | xargs perl -p -i -e "s/red/blue/g"
done |
xargs perl -p -i -e "s/red/blue/g"

Incidentally, I'd use single quotes here; 顺便说一句,我在这里使用单引号。 I always use them whenever the quoted string is to be taken literally. 每当引号字符串按字面意义使用时,我总是使用它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “寻找。 -regex ...” 在 Python 或如何查找全名(路径+名称)与正则表达式匹配的文件? - “find . -regex …” in Python or How to find files whose whole name (path + name) matches a regular expression? 如何检查字符串是否与perl中的正则表达式匹配? - How to check if string matches a regular expression in perl? 如何修改此正则表达式,以便它只匹配目录并排除文件? - How to modify this regex so that it only matches directories and excludes files? Emacs:将名称与正则表达式匹配的当前目录下的文件删除吗? - Emacs: Dired the files under the current directory whose name matches a regular expression? ant:删除名称全为数字的目录 - ant : delete directories whose name is entirely numeric 尝试使用 GNU find 递归搜索仅在文件名的任何部分包含字符串的文件名(不是目录) - Trying to use GNU find to search recursively for filenames only (not directories) containing a string in any portion of the file name 根据正则表达式递归重命名目录和文件 - Recursively rename directories and files based on a regular expression 如何在Perl中计算两个结构不同的文件中的匹配数 - How to count the number of matches in two files of different structures in Perl 如何将字符串转换为在Perl中与自身匹配的正则表达式? - How can I convert a string into a regular expression that matches itself in Perl? 如何排除与Perl正则表达式匹配的字符串部分? - How can I exclude the part of the string that matches a Perl regular expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM