4

I work on macOS High Sierra 10.13.3. My shell is bash.

When I type, echo {1,2}{3,4}

I get: 13 14 23 24.

Is there an option to get 13 24 only?

I am interested in creating many files where two locations in a file vary together. For example, I want the files:

file1file1 file2file2 file3file3

It would be convenient to write something like file{1,2,3}file{1,2,3} option instead of file1file1 file2file2 file3file3.

I would like to be able to use this expansion in a command, such as:

touch file{1,2,3}file{1,2,3} option to create three files.

I hope that the functionality I am looking for is clear.

Clarification

Ultimately, the context I want to use this functionality in is with a snakemake command:

snakemake --cores 3 release{42,43,44}/file{42,43,44}.txt

where I want snakemake to produce the files release42/file42.txt, release43/file43.txt and release44/file44.txt.

If I use a loop to achieve this, the files will be produced in succession. However, by typing snakemake release42/file42.txt release43/file43.txt release44/file44.txt, the three files will be produced simultaneously. However, as I am lazy, I want to type something shorter than snakemake release42/file42.txt release43/file43.txt release44/file44.txt.

charlesdarwin
  • 717
  • 1
  • 9
  • 16

2 Answers2

3

Use a loop to form an array.

items=( )
for i in {42,43,44}; do
  items+=( "release$i/file$i.txt" )
done

snakemake "${items[@]}"

This runs snakemake only once, with all the files listed.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
2

to get 13 14 do

$ echo 1{3,4}

for the duplicates, there are other ways

$ for i in {1..3}; do printf "file%dfile%d " $i $i; done
file1file1 file2file2 file3file3

to use with touch

$ for i in {1..3}; do touch "file${i}file${i}"; done

or, using all filenames at once

$ touch $(for i in {1..3}; do printf "file%dfile%d " $i $i; done)
karakfa
  • 66,216
  • 7
  • 41
  • 56
  • for the 13 14, I made a mistake in my question, sorry. Regarding your other answer, it's printing it but I'd like to be able to use this in a command, to create the files for example. – charlesdarwin Feb 17 '19 at 23:56
  • how is `echo` command creating files? Not sure I got what you mean. `printf` is just more powerful `echo`. – karakfa Feb 17 '19 at 23:57
  • I've made my question more precise by adding what I want to do with the `touch` command. – charlesdarwin Feb 17 '19 at 23:59
  • I understand I could use a for loop with this as well, but to create many files, I think it would be more efficient to expand braces to create them all at once. Furthermore, I need this functionality in another context (launching a snakemake command), where I would like all output files in one command. – charlesdarwin Feb 18 '19 at 00:01
  • added more details about what I want to achieve in the clarification. – charlesdarwin Feb 18 '19 at 00:07
  • The last approach with the unquoted expansion is fragile. Any filename with spaces -> boom. (Safe for this specific case where we control all characters in the output with sane values for IFS, but better to describe the limitations of a potentially-harmful practice when showcasing it so it doesn't get applied outside that limited safe scope). – Charles Duffy Feb 18 '19 at 13:53