gpt4 book ai didi

Linux Ubuntu Bash - 使用 AWK 正则表达式查找包含 2 个以上元音的单词

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:11:03 25 4
gpt4 key购买 nike

我想使用 awk 从一个文件中打印所有包含超过 2 个元音的单词。

到目前为止,这是我的代码:

#!/bin/bash
cat $1 | awk '{ #Default file separator is space
for (i=1;i<=NF;i++) #for every word
{
if ($i ~ /([aeiojy]){2,}/)
{
print $i
}
}}'

正则表达式是问题

/([aeiojy]){2,}/) 这是我的真实想法,但行不通。

最佳答案

这应该适用于 GNU grep:

grep -Poi '([^[:space:]]*?[aeiou]){3,}[^[:space:]]*' file

选项:

-P perl compatible regular expressions
-o output every match on a single line
-i case insensitive match

正则表达式:

(                start of subpattern
[^[:space:]]* zero or more arbitrary non whitespace characters
? ungreedy quantifier for the previous expression (perl specific)
[aeiou] vowel
) end of subpattern
{3,} the previous expression appears 3 or more times
[^[:space:]]* zero or more other characters until word boundary.

顺便说一句,这里实际上不需要 perl 兼容的正则表达式。使用普通的 grep 你可以使用:

grep -oi '\([^[:space:]aeiou]*[aeiou]\)\{3,\}[^[:space:]]*' file

注意:我在上面的示例中排除了标点符号,但如果需要可以添加。

关于Linux Ubuntu Bash - 使用 AWK 正则表达式查找包含 2 个以上元音的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36901571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com