Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to iterate both on alphabet and integer in a bash loop

My code

for ((i={A..Z}, j=30 ; i < $words2 + 1, j < (30 + (20 * $words2)) ; i++, j+=20)) ; 
do
  printf '%s %s %s\n' '<text x="'$j'" y="10">'$i'</text>' | sed 's/ *$//' 
done

and the output that I get is

<text x="30" y="10">0</text>
<text x="50" y="10">1</text>
<text x="70" y="10">2</text>
<text x="90" y="10">3</text>

I would like to go over the alphabet until i is less than $words2 but i apparently stays a letter.
My desired output is this where i is equal to the alphabet in uppercase

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<text x="30" y="10">A</text>
<text x="50" y="10">B</text>
<text x="70" y="10">C</text>
<text x="90" y="10">D</text>

>Solution :

Like this:

arr=( {A..Z} )
for ((i=0, j=30; i<=4, j<=70; i++, j+=20)); do
    printf '<text x="%d" y="10">"$%s"</text>\n' $j "${arr[i]}"
done 

Output

<text x="30" y="10">"$A"</text>
<text x="50" y="10">"$B"</text>
<text x="70" y="10">"$C"</text>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading