简体   繁体   English

在单词中查找第一个元音 - bash

[英]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.我已经使用常规编程语言找到了这个问题的其他几个答案,但没有找到 bash 的答案。我正在尝试编写一个 bash 脚本,但遇到了能够在给定中找到第一个元音的问题细绳。 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.我的目标是查看一个单词是否以一个或多个辅音开头,然后将这些辅音移动到单词的末尾并添加“ay”,然后打印出单词。 Any help is appreciated.任何帮助表示赞赏。

One idea using sed and regex/capture groups:使用sed和正则表达式/捕获组的一个想法:

$ 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 sed -E - 在扩展正则表达式模式下运行
  • (^[^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 ) (^[^aeiouAEIOU]*) - 第一个捕获组从输入的开头开始(第一个^ )并包括直到第一个元音的所有内容(即,不在aeiouAEIOU中的所有内容)
  • (.*) - second capture group grabs the rest of the input (ie, from the first vowel to the end of the input) (.*) - 第二个捕获组获取输入的 rest(即从第一个元音到输入的末尾)
  • \2\1ay - output 2nd capture group ( \2 ), then the 1st capture group ( \1 ), then 'ay ' \2\1ay - output 第二个捕获组 ( \2 ),然后是第一个捕获组 ( \1 ),然后'ay '
  • <<< 'nix' - a 'here string' that allows us to feed a string as stdin to the sed command <<< 'nix' - 允许我们将字符串作为标准输入提供给sed命令的“此处字符串”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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