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

时间:2022-06-01 23:19:05

I have a general question regarding pattern matching in Linux. Say, I have a script through which I want to run many files in succession, that is run like this:

我有一个关于Linux中模式匹配的一般问题。说,我有一个脚本,我希望连续运行许多文件,运行方式如下:

./script -i file1 -j file2

I have many pairs of files which are identical in name apart from the last two characters, for example file1 ends in -1 and file2 in -2.

我有许多文件对,除了最后两个字符之外,它们的名称相同,例如file1以-1结尾,file2以-2结尾。

- does not appear in any other place of the filename so I am thinking there should be a way of piping all the pairs of files through the script using regular expressions?

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

1 个解决方案

#1


1  

If you have your folder with all the files:

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

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

do the following:

请执行下列操作:

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

#1


1  

If you have your folder with all the files:

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

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

do the following:

请执行下列操作:

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