Home > Mobile >  How to go over each letter of a word and change it in bash
How to go over each letter of a word and change it in bash

Time:12-17

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

 <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

CodePudding user response:

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"

CodePudding user response:

We'd need more information to be able to debug whatever problem you're having but here's how to really do what you appear to be trying to do using any awk:

$ cat tst.sh
#!/usr/bin/env bash

awk '
    {
        lgth = length($0)
        for ( i=1; i<=lgth; i   ) {
            char = substr($0,i,1)
            fill = (char == "#" ? "black" : "white")
            printf "<rect x=\"20\" y=\"20\" fill=\"%s\" />\n", fill
        }
        print ""
    }
' "${@:--}"

$ ./tst.sh file
<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" />

or maybe:

$ cat tst.sh
#!/usr/bin/env bash

awk '
    {
        lgth = length($0)
        for ( i=1; i<=lgth; i   ) {
            char = substr($0,i,1)
            fill = ""
            if ( char == "#" ) {
                fill = "black"
            }
            else if ( char ~ /[A-Z]/ ) {
                fill = "white"
            }
            if ( fill != "" ) {
                printf "<rect x=\"20\" y=\"20\" fill=\"%s\" />\n", fill
            }
        }
        print ""
    }
' "${@:--}"

,p>

$ ./tst.sh file
<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" />
  •  Tags:  
  • bash
  • Related