![用于提取第二个单词只有两个元音的行的 Grep 模板](https://linux22.com/image/100669/%E7%94%A8%E4%BA%8E%E6%8F%90%E5%8F%96%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%8D%95%E8%AF%8D%E5%8F%AA%E6%9C%89%E4%B8%A4%E4%B8%AA%E5%85%83%E9%9F%B3%E7%9A%84%E8%A1%8C%E7%9A%84%20Grep%20%E6%A8%A1%E6%9D%BF.png)
例如,我有包含内容的文件
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