-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

2
  • 2
    for i in {001..022}; do
    – Cyrus
    Aug 26, 2020 at 16:50
  • 1
    the question you link to is enough information to solve this problem. Aug 26, 2020 at 16:50

1 Answer 1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.