Bash script to extract 10 most common double-vowels word form a file

Viewed 88

So I have tried to write a Bash script to extract the 10 most common double-vowels words from a file, like good, teeth, etc. Here is what I have so far:

grep -E -o '[aeiou]{2}' $1|tr 'A-Z' 'a-z' |sort|uniq -c|sort -n | tail -10

I tried to use grep with flag E, then find the pattern match, such as 'aa', 'ee', 'ii' , etc, but it is not working at all, enter image description here, what I got back, just 'ai', 'ea', something like this. Can anyone help me figure how to do pattern match in bash script?

3 Answers

Simple way to change your regex: replace [aeiou]{2} with aa|ee|ii|oo|uu. (This does not fix the issue of only finding the match rather than the full word.)

Building on Andrew's answer (re: matching double vowels):

$ cat words.txt
good food;foul make chicken,eek too brave
eye you yuu something:three food too tu too

$ grep -E -o '\<[[:alnum:]]*(aa|ee|ii|oo|uu)[[:alnum:]]*\>' words.txt
good
food
eek
too
yuu
three
food
too
too

The grep finds only words (\< and \> represent word boundaries) with letters and/or digits and containing a dual vowel, printing each word on a separate line.

Applying the rest of OP's counting/sorting logic:

$ grep -E -o '\<[[:alnum:]]*(aa|ee|ii|oo|uu)[[:alnum:]]*\>' words.txt | sort | uniq -c | sort -n
      1 eek
      1 good
      1 three
      1 yuu
      2 food
      3 too

You can simply match any amount of letters before or after a repeated vowel with this POSIX ERE regex with a GNU grep:

grep -oE '[[:alpha:]]*([aeiou])\1[[:alpha:]]*' words.txt

FreeBSD (non-GNU) grep does not support a backreference in the pattern, so you will have to list all possible vowel sequences:

grep -oE '[[:alpha:]]*(aa|ee|ii|oo|uu)[[:alpha:]]*' words.txt

See the online demo:

#!/bin/bash
s='Some good feed
Soot and weed'
grep -oE '[[:alpha:]]*([aeiou])\1[[:alpha:]]*' <<< "$s"

Details:

  • [[:alpha:]]* - zero or more letters
  • (aa|ee|ii|oo|uu) - one of the char sequences, aa, ee, ii, oo or uu (| is an alternation operator in a POSIX ERE regex)
  • ([aeiou]) - Group 1: a vowel
  • \1 - the same vowel as in Group 1
  • [[:alpha:]]* - zero or more letters

See the diagram:

enter image description here

Related