简体   繁体   中英

Check if a string contains element of array with a shell script

I have an array containing various substring

array=(Jack Jessy Harold Ronald Boston Naomi)

and i have a string containing a paragraph

text=" Jack is maried with Jessy, they have 3 children Ronald Boston and Naomi and Harold is the last one "

I want to check using bash if the text contain all the strings that are inside the array but in a different way at the moment I can get them like that

if [[ $text == *${array[0]}* && $text == *${array[1]}* && $text == *${array[2]}* && $text == *${array[3]}* && $text == *${array[4]}* && $text == *${array[5]}*  ]]; then
  echo "It's there!"
fi

A more reusable way:

#!/usr/bin/env bash

array=(Jack Jessy Harold Ronald Boston Naomi)

text=" Jack is maried with Jessy, they have 3 children Ronald Boston and Naomi and Harold is the last one "

check(){
    local str string=" $1 "; shift
    MAPFILE=()
    for str; do
        pattern="\b$str\b" # Word search, excluding [Jacky], for example
        [[ $string =~ $pattern ]] || MAPFILE+=($str)
    done
    test ${#MAPFILE[@]} = 0
}

if  check "$text" "${array[@]}"; then
    echo "All in"
else
    echo "Not all in : [${MAPFILE[@]}]"
fi

Try this. Loop through your array and mach $item with $text

array=(Jack Jessy Harold Ronald Boston Naomi Bill Marry)
text=" Jack is maried with Jessy, they have 3 children Ronald Boston and Naomi and Harold is the last one "

$ for item in ${array[@]}; { [[ "$text" =~ $item ]] && echo yep $item is here || echo nop can\'t find $item; }
yep Jack is here
yep Jessy is here
yep Harold is here
yep Ronald is here
yep Boston is here
yep Naomi is here
nop can't find Bill
nop can't find Marry

Update, to summarize results

err=0; for item in ${array[@]}; { [[ "$text" =~ $item ]] || ((err++)); }; ((err>0)) && echo someone is missing || echo all there

Or like so to see who is missing

for item in ${array[@]}; { [[ "$text" =~ $item ]] || missing+=($item); }; [[ ${missing[@]} ]] && echo can\'t find ${missing[@]} || echo all there

A shell is an environment from which to call tools, not a tool to manipulate text. The guys who invented shell also invented awk for shell to call to manipulate text. The awk script included below will work clearly, robustly, efficiently, and portably using any awk in any shell on every UNIX box:

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

checkWordsInSentence() {
    awk -v wordsStr="${array[*]}" -v sentenceStr="$text" 'BEGIN {
        split(sentenceStr,words,/[^[:alpha:]]+/)
        for (i in words) {
            sentence[words[i]]
        }
        split(wordsStr,words)
        for (i in words) {
            word = words[i]
            totWords++
            if (word in sentence) {
                status[word] = "present"
                numPresent++
            }
            else {
                status[word] = "absent"
            }
        }
        if (totWords == numPresent) {
            printf "All %d words (%s) present\n", totWords, wordsStr
        }
        else {
            printf "%d of %d words (%s) present\n", numPresent, totWords, wordsStr
            for (word in status) {
                print word, status[word]
            }
        }
    }'
}

array=(Jack Jessy Harold Ronald Boston Naomi)
text=" Jack is maried with Jessy, they have 3 children Ronald Boston and Naomi and Harold is the last one "
checkWordsInSentence

echo '----'

array=(Jack Jessy Harold Bob Ronald Boston Naomi Karen)
text=" Jack is maried with Jessy, they have 3 children Ronald Boston and Naomi and Harold is the last one "
checkWordsInSentence

.

$ ./tst.sh
All 6 words (Jack Jessy Harold Ronald Boston Naomi) present
----
6 of 8 words (Jack Jessy Harold Bob Ronald Boston Naomi Karen) present
Karen absent
Ronald present
Bob absent
Jack present
Boston present
Naomi present
Jessy present
Harold present

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