我有一个关于 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/