'How to check all multiple keyword should be present in given text or not
I am bit stuck with this how do I check all keyword should exist in my text
If any one keyword is not present then it should return me status : 1 or else 0
keyword='CAP\|BALL\|BAT\|CRICKET'
echo "HE AS CAP AND LOVE TO PLAY BALL BAT , ITS IS CALLED CRICKET"
My code :
echo "HE AS CAP AND LOVE TO PLAY BALL BAT , ITS IS CALLED CRICKET" | grep -w "$keyword"
echo $?
Solution 1:[1]
Here is another way to achieve this using shell and grep:
diffSets() {
[[ -n $2 && -z $( grep -vxFf <(echo "${1// /$'\n'}") <(echo "${2//|/$'\n'}") ) ]]
}
This function first replaces all spaces with newlines in first set and replaces all pipes with newlines in second set. Then it uses process substitution with grep -vxFf to get not-matching text from second set using first set as search patterns.
Testing:
keyword='CAP|BALL|BAT|CRICKET'
s="HE AS CAP AND LOVE TO PLAY BALL BAT , ITS IS CALLED CRICKET"
diffSets "$s" "$keyword" && echo "all" || echo "not all"
all
keywork='CAP|BALL|BAT|CRICKET|FOO'
diffSets "$s" "$keywork" && echo "all" || echo "not all"
not all
diffSets "$s" "" && echo "all" || echo "not all"
not all
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
