How do I print the second vowel in a word in red or blue color. Let say I execute my script called "script1" which reads a text file. My script should be able to print the second vowel in every word in red or blue color. If there is only one vowel, then print that in color. If there are no vowels then print the first letter in a word in color!

  • What is the problem: how to colour the output, or how to recognise the second vowel? (Or both?)
    – choroba
    Mar 22 '15 at 14:13
  • I have a script which reads a text file called file1.txt prints each word one by one. What I want is to print the second vowel in every word in either blue or red color. If a word contain only one vowel then print that vowel in blue or red color, if a word has no voewls at all then print the first letter in that word in blue or red color! Mar 22 '15 at 14:37

filename="./file" # change this filename according to your requirements

# read single line from $filename in $line (see last "done", last line of code)
while read -r line; do 

  # loop over every single word in $line
  for word in $line; do
    vc=$(tr -cd '[aeiouAEIOU]' <<< "$word") # remove all but vowels

    case ${#vc} in                          # ${#vc}: number of vowels
      0)                                    # no vowel
        sed -E "s/^./${red}&${reset}/" <<< "$word"
      1)                                    # one vowel
        sed -E "s/^([^aeiou]*)([aeiou]+)(.*)$/\1${red}\2${reset}\3/i" <<< "$word"
      *)                                    # two or more vowels
        sed -E "s/^([^aeiou]*[aeiou][^aeiou]*)([aeiou])(.*)$/\1${red}\2${reset}\3/i" <<< "$word"

  done | tr "\n" " "
done < "$filename"

Disadvantage: You loose consecutive whitespaces and tabs in output.

More information: help <command> or man <command> or info <command>

  • Can you please explain how the while and for loops are working in this script so I can better understand the solution! Thanks Mar 22 '15 at 15:55
  • I've updated my answer.
    – Cyrus
    Mar 22 '15 at 16:02
  • I've fixed a bug with consecutive vowels.
    – Cyrus
    Mar 22 '15 at 16:58
  • Thanks a lot! I have a loop which prints one word or characters from a text file one at a time. I tried to put this together so that it can work with my loop, but it does not do anything! Mar 22 '15 at 17:28
  • Here is the loop:FS=$'\n' for j in cat $1; do #Reads the text file in the centre of the screen echo " ___________________" echo " $j"; echo " ___________________" echo " Speed 120 wpm" sleep 0.5; clear; done Mar 22 '15 at 17:28

How about using grep to colour the match, along with some PCRE-fu? something like

grep --color=always -iP '\b[^aeiou\s]*[aeiou][^aeiou\s]*\K[aeiou]'


  • \b to match a word-boundary; then
  • [^aeiou\s]* match zero or more non-vowel, non-space characters
  • [aeiou] to match the first vowel
  • [^aeiou\s]* match another zero or more non-vowel, non-space characters
  • \K to exclude all the above from the final matched expression (effectively, a variable length lookbehind assertion)
  • finally, [aeiou] to match the desired second vowel

enter image description here

  • 2
    It does not completely fulfill the OP request. You need to highlight the only vowel in the word as well, and the first letter if there are no vowels.
    – LDC3
    Mar 22 '15 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.