Search code examples
bashinputgrepword-list

Detect whether sentence contains a word in a file


When the user enters something, the script is suppose to check if the input contains any of the words from the wordfile.

Input:

A sentence that contains the word, word1.

Wordfile:

word1
word2
word3

Code:

[[ -z $(cat 'vocabulary/lists/wordfile' | grep -Fe $($input//" "/"\|")) ]]

Output:

> [WORD]
./core.sh: line 168: [WORD]// /\|: No such file or directory
grep: option requires an argument -- 'e'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
[WORD]

Solution

  • Try this :

    [[ -n $(sed -n -f <(sed 's/^/\//g;s/$/\/p/g' vocabulary/lists/wordfile) <<< "$input") ]] && echo Found