regex - 如何在匹配正则表达式的多个文件上运行脚本?

标签 regex linux bash

我有一个关于 Linux 模式匹配的一般性问题。 说,我有一个脚本,我想通过它连续运行许多文件,它是这样运行的:

./script -i file1 -j file2

我有许多文件对,除了最后两个字符外名称完全相同,例如 file1 以 -1 结尾和 file2 在 -2 .

-没有出现在文件名的任何其他地方,所以我认为应该有一种方法可以使用正则表达式通过脚本管道传输所有文件对?

最佳答案

如果您的文件夹中包含所有文件:

$ ls
fileA-1   fileB-1   fileC-1
fileA-2   fileB-2   fileC-2

执行以下操作:

for file1 in *-1
do
    # This will remove the trailing '1' from the file and append a '2'
    # learn more about parameter substitution at
    # http://tldp.org/LDP/abs/html/parameter-substitution.html
    file2="${file1%1}2"

    #execute!
    ./script -i "$file1" -j "$file2"
done

关于regex - 如何在匹配正则表达式的多个文件上运行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17942330/

相关文章:

php - PHP 中数学方程的正则表达式

java - 优化大量 Scanner.findWithinHorizo​​n(pattern, 0) 调用

bash - 如何添加具有升序名称的目录,例如 dir-000、dir-001 等

bash - 为什么我的程序需要这么长时间才能运行?

python - 在 python 风格中使用正则表达式来匹配字符串中的一个(或全部)bash 波浪号前缀?

regex - 替换 html 参数中的模式

regex - Powershell 替换正则表达式

linux - Getopts 默认情况下的 bash 脚本

c++ - 如何获取 C/C++ 中 Linux 系统调用的输出?

regex - Linux grep 命令用于查找以 ? 开头的单词特点