简体   繁体   中英

How to match string values from text file in bash script

I am trying to read and display each word in a text file and then highlight a specified vowel or consonant.

When I read from the file I can only read an entire line so I used a loop to read each word in the line. However when I do this I'm not able to access different positions in that word. (eg if the word is "happy" I cannot access the 2nd positon for the letter "a")

#READS EACH LINE
while read line
    do
            #READS EACH WORD IN THE LINE
            for word in $line
            do
                    #LOOPS THROUGH EACH CHARACTER IN THE WORD
                    for (( i=0; i<${#word}; i++ ))
                    do
                            #LOOPS THROUGH ALL VALUES IN VOWEL ARRAY
                            for (( j=0; j<10; j++ ))
                            do
                                    #MATCHES IF A VOWEL IS FOUND
                                    if [ "${word[$i]}" == "${varray[$j]}" ]
                                    then
                                            let vcount++
                                            echo i:$i  j:$j
                                            echo word: ${word[$i]} varray: ${varray[$j]}    #DEBUG

                                    fi
                            done
                    done
                                            #MORE CODE HERE DOING STUFF PRINTING TO SCREEN
            done
    done < $file

Is there something I am doing wrong? or should I go about reading the file a different way in order to access each character of the word?

When I echo the word in the double for loop it displays the entire word as the first position instead of just the first character. And the rest of the positions 2, 3, etc are empty.

You seem to be confusing array indexing with taking the substring of a string ("substringing", if you will). Those are completely distinct operations in every programming language I'm familiar with. In bash, array indexing is done with the ${arr[index]} form of variable substitution. What you need is the ${str:index:length} form of variable substitution, which does substringing. Here's how it can be used to make your script work:

#!/bin/bash

file='file';
varray='aeiouy';

#READS EACH LINE
while read line; do
    #READS EACH WORD IN THE LINE
    for word in $line; do
        #LOOPS THROUGH EACH CHARACTER IN THE WORD
        for (( i=0; i<${#word}; i++ )); do
            #LOOPS THROUGH ALL VALUES IN VOWEL ARRAY
            for (( j=0; j<${#varray}; j++ )); do
                #MATCHES IF A VOWEL IS FOUND
                wordChar="${word:$i:1}";
                vChar="${varray:$j:1}";
                if [[ "$wordChar" == "$vChar" ]]; then
                    let vcount++;
                    echo "i:$i  j:$j";
                    echo "wordChar: $wordChar vChar: $vChar";
                fi;
            done;
        done;
    done;
    #MORE CODE HERE DOING STUFF PRINTING TO SCREEN
done <$file;

exit 0;

Demo:

cat file;
## abc def ghi
## jkl mno pqr

./script;
## i:0  j:0
## wordChar: a vChar: a
## i:1  j:1
## wordChar: e vChar: e
## i:2  j:2
## wordChar: i vChar: i
## i:2  j:3
## wordChar: o vChar: o

Other notes:

  • Your code to read from the file and break it up into words is just fine. The only caveat is that the read line command will lose leading and trailing whitespace. This is not important for your script, but if you wanted to solve this, you could just call read by itself, and the entire line (including leading and trailing whitespace) would be read into the $REPLY variable by default.
  • In your code, you iterated j from 0 to 9, but the max depends on the length of varray (which is actually a string; should probably rename that), so you should use ${#varray} as the max.
  • Use [[ ... ]] for expression evaluation instead of the old [ ... ] form; the newer form is slightly more powerful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM