-2

Based off this: How to zero pad a sequence of integers in bash so that all have the same width?

I need to create new file names to enter into an array representing chromosomes 1-22 with three digits (chromsome001_results_file.txt..chromsome022_results_file.txt)

Prior to using a three digit system (which sorts easier) I was using

for i in {1..22}; 
do echo chromsome${i}_results_file.txt; 
done 

I have read about printf and seq but was wondering how they could be put within the middle of a loop surrounded by text to get the 001 to 022 to stick to the text.

Many thanks

tacrolimus
  • 500
  • 2
  • 12

1 Answers1

1

Use printf specifying a field with and zero padding.

for i in {1..22}; 
do 
    printf 'chromsome%03d_results_file.txt\n' "$i"
done 

In %03d, d means decimal output, 3 means 3 digits, and 0 means zero padding.

Barmar
  • 741,623
  • 53
  • 500
  • 612