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 go over each letter of a word and change it in bash

My code looks like this

foo=`cat $words`
for (( i=0; i<${#foo}; i++ )); do
    new=${foo:$i:1}
    while IFS=read -r line 
    do
        if [[ $new=="#" ]]
        then 
            echo "<rect x="20" y="20" fill="black" />"
        elif [[ $new==[A-Z] ]]
        then
            echo "<rect x="20" y="20" fill="white" />"
        fi
    done < "$new"
done

I get an error message containing the following

crossword: line 21: F: No such file or directory
2       crossword: line 21: G: No such file or directory
3       crossword: line 21: #: No such file or directory
4       crossword: line 21: 
5       : No such file or directory
6       crossword: line 21: Z: No such file or directory
7       crossword: line 21: #: No such file or directory
8       crossword: line 21: E: No such file or directory
9       crossword: line 21: 

My desired output should have looked like this

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

 <rect x="20" y="20" fill="white" />
 <rect x="20" y="20" fill="white" />
 <rect x="20" y="20" fill="black" />

 <rect x="20" y="20" fill="white" />
 <rect x="20" y="20" fill="black" />
 <rect x="20" y="20" fill="white" />

Because the content of the file is

FG#
Z#E

Where # will be replaced by black and the letters with white

>Solution :

This entire thing can be done with a single sed command:

sed -e 's|#|<rect x="20" y="20" fill="black" />\n|g' -e 's|[A-Z]|<rect x="20" y="20" fill="white" />\n|g' "$words"
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