简体   繁体   中英

Find first vowel in word - bash

I've found a couple of other answers for this question using regular programming languages but not one for bash. I'm trying to write a bash script and I've run into the problem of being able to locate the first vowel in a given string. My goal is to see if a word begins with one or more consonants and then move those consonants to the end of the word and add "ay" and then print out the word. Any help is appreciated.

One idea using sed and regex/capture groups:

$ sed -E 's/(^[^aeiouAEIOU]*)(.*)/\2\1ay/g' <<< 'nix'
ixnay
$ sed -E 's/(^[^aeiouAEIOU]*)(.*)/\2\1ay/g' <<< 'NIX'
IXNay
$ sed -E 's/(^[^aeiouAEIOU]*)(.*)/\2\1ay/g' <<< 'potato'
otatopay
$ sed -E 's/(^[^aeiouAEIOU]*)(.*)/\2\1ay/g' <<< 'school'
oolschay

Where:

  • sed -E - run in extended regex mode
  • (^[^aeiouAEIOU]*) - first capture group starts at the beginning of the input (first ^ ) and includes everything up to the first vowel (ie, everything that is not in aeiouAEIOU )
  • (.*) - second capture group grabs the rest of the input (ie, from the first vowel to the end of the input)
  • \2\1ay - output 2nd capture group ( \2 ), then the 1st capture group ( \1 ), then 'ay '
  • <<< 'nix' - a 'here string' that allows us to feed a string as stdin to the sed command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM