Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash capturing in brace expansion

What would be the best way to use something like a capturing group in regex for brace expansion. For example:

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

results in all permutations of the numbers and 25 different files. But in case I just want to have files like 1myfile1.txt, 2myfile2.txt,... with the first and second number the same, this obviously doesn't work. Therefore I'm wondering what would be the best way to do this? I'm thinking about something like capturing the first number, and using it a second time. Ideally without a trivial loop.

Thanks!

like image 347
avermaet Avatar asked Dec 06 '25 08:12

avermaet


1 Answers

Not using a regex but a for loop and sequence (seq) you get the same result:

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

Or tidier:

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

As an example, using echo instead of touch:

➜ for i in $(seq 1 5); do echo ${i}myfile${i}.txt; done
1myfile1.txt
2myfile2.txt
3myfile3.txt
4myfile4.txt
5myfile5.txt
like image 127
Gonzalo Matheu Avatar answered Dec 08 '25 22:12

Gonzalo Matheu