So I need to match a string in given word list. For example, if I give the input like

$ sh match_the_pattern.sh -i aei words.txt

It should match the character "a" "e" and "i" in order.

the alphabets in the string must occur in the word in that order, but that other characters may occur in between.

Note : the character string may change and the dictionary may change.

My Approach :

  1. Read the input
  2. Scan for the filename
  3. SED or GREP combination to match the character string
  4. Output

What I do not know? the 3rd part.

while [[ $# -gt 0 ]]; do
  case $1 in
       -i)
    arg=$2
    egrep "*[$argument]*" $name
    shift ;;
  esac
  shift
done

What did not work

a+.*e+.*i+.*

sh match_the_pattern.sh -i aeiou words.txt

adventitious
adventitiousness
sacrilegious
abstemious
sacrilegiousness

if you notice, a,e,i,o,u are in order one after another. That is what I want. the character string which we are going to match may change.

1 Answers

1
oguzismail On Best Solutions

Assuming $argument should contain only letters; generate the pattern using sed, match words using grep:

grep "$(sed 's/./&.*/g' <<< "$argument")" "$name"