gpt4 book ai didi

Bash 在大括号扩展中捕获

转载 作者:行者123 更新时间:2023-11-29 09:36:00 25 4
gpt4 key购买 nike

在正则表达式中使用捕获组之类的东西进行大括号扩展的最佳方法是什么。例如:

touch {1,2,3,4,5}myfile{1,2,3,4,5}.txt

产生数字的所有排列和 25 个不同的文件。但是如果我只想拥有像 1myfile1.txt2myfile2.txt 这样的文件,...并且第一个和第二个数字相同,这显然是行不通的.因此,我想知道最好的方法是什么?我正在考虑诸如捕获第一个数字并再次使用它之类的事情。理想情况下没有简单的循环。

谢谢!

最佳答案

不使用正则表达式而是使用 for loop和序列(seq)你得到相同的结果:

for i in $(seq 1 5); do touch ${i}myfile${i}.txt; done

或更整洁:

 for i in $(seq 1 5); 
do
touch ${i}myfile${i}.txt;
done

例如,使用 echo 而不是 touch:

➜ for i in $(seq 1 5); do echo ${i}myfile${i}.txt; done
1myfile1.txt
2myfile2.txt
3myfile3.txt
4myfile4.txt
5myfile5.txt

关于Bash 在大括号扩展中捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58219779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com