用于提取第二个单词只有两个元音的行的 Grep 模板

用于提取第二个单词只有两个元音的行的 Grep 模板

例如,我有包含内容的文件

hello world
it's nice to see you
amazing night
what a wonderful day
my name is Robert
still breathing
speaking bottom soul
something wrong

我需要匹配这些行,其中第二个单词恰好有两个元音。所以输出应该是:

it's nice to see you
my name is Robert
speaking bottom soul

我怎样才能使用 grep 来做到这一点?

答案1

grep 与扩展正则表达式:

grep -iE '^[^[:blank:]]+[[:blank:]]+([^aeiou]*[aeiou]){2}[^aeiou]*\>' file

grep 与 PCRE

grep -iP '^\S+\s+([^aeiou]*[aeiou]){2}[^aeiou]*\b' file

perl(老实说,我这样做与steeldriver的评论无关)

perl -ane 'print if (lc($F[1]) =~ tr/aeiou/aeiou/) == 2' file

awk

awk '{col2 = tolower($2); gsub(/[aeiou]/,"",col2)} length($2) - length(col2) == 2' file

相关内容