【问题标题】:How can I find all the words in a file that contain every vowel? [duplicate]如何在包含每个元音的文件中找到所有单词? [复制]
【发布时间】:2020-12-06 18:55:50
【问题描述】:

myfile.txt 中,我想获取包含每个元音至少出现一次的单词列表 [aeoui](不区分大小写)。我最好用grep 来做这件事。

我的猜测是从以下命令开始,获取单词列表,但我不知道在它之后放置什么管道以获得想要的结果。

grep -Eo "\w+"

【问题讨论】:

  • sed -E 's/[[:space:]]+/\n/g' myfile.txt | grep a | grep e | grep i | grep o | grep u?或 sed -E s/[[:space:]]+/\n/g' myfile.txt | awk '/a/ && /e/ && /i/ && /o/ && /u/' 用于非 grep 方法。
  • 上述欺骗中的正则表达式与grep -iP ...

标签: linux bash awk grep


【解决方案1】:

假设:

  • OP 已确定 grep -Eo "\w+" 返回准确的 'words' 列表

一些样本数据:

$ cat vowels.dat
this is a test.
and, this, in another test
what about hyphen-ated-words?
some nonsense to match XaXeXiXoXuX
more non-sense xAxExIxOxUx

OP 的 grep 应用于此数据:

$ grep -Eo "\w+" vowels.dat
this
is
a
test
and
this
in
another
test
what
about
hyphen
ated
words
some
nonsense
to
match
XaXeXiXoXuX          # contains all 5x vowels
more
non
sense
xAxExIxOxUx          # contains all 5x vowels

一个awk解决方案:

awk '
BEGIN { split("aeiou",vowels,"") }                # populate an array of lowercase vowels
      { cnt=0                                     # reset our match counter
        lcword=tolower($0)                        # convert our word to lower case (takes care on case-insensitive requirement)
        for ( i in vowels )                       # loop through array of vowels
            if ( lcword ~ vowels[i] )             # if our lowercase input contains the current loop/vowel ...
               cnt++                              # increment our match counter
        if ( cnt == 5 )                           # if we have 5 matches ...
           print $0                               # print the current word to stdout
      }
' <(grep -Eo "\w+" vowels.dat)

这会生成:

XaXeXiXoXuX
xAxExIxOxUx

【讨论】:

    【解决方案2】:

    第一种解决方案:考虑到 OP 想要打印具有所有元音的单词,然后尝试跟随。

    awk '
    {
      for(i=1;i<=NF;i++){
        if((sub(/[Aa]/,"&",$i)+sub(/[Ee]/,"&",$i)+sub(/[Ii]/,"&",$i)+sub(/[Oo]/,"&",$i)+sub(/[Uu]/,"&",$i))==5){
          print $i
        }
      }
    }'  Input_file
    

    说明:为上述添加详细说明。

    awk '                      ##Starting awk program from here.
    {
      for(i=1;i<=NF;i++){      ##Looping through all fields here.
        if((sub(/[Aa]/,"&",$i)+sub(/[Ee]/,"&",$i)+sub(/[Ii]/,"&",$i)+sub(/[Oo]/,"&",$i)+sub(/[Uu]/,"&",$i))==5){      ##Checking condition if substituting aA|eE|iI|oO|uU has cout 5 in current field(means all are found) then do following.
          print $i             ##printing current field here.
        }
      }
    }'  Input_file             ##mentioning Input_file name here.
    


    第二个解决方案:如果您想打印其中包含所有元音的单词的数量,请尝试以下操作。

    awk '
    {
      for(i=1;i<=NF;i++){
        if((sub(/[Aa]/,"&",$i)+sub(/[Ee]/,"&",$i)+sub(/[Ii]/,"&",$i)+sub(/[Oo]/,"&",$i)+sub(/[Uu]/,"&",$i))==5){
          count++
        }
      }
    }
    END{
      print "Total="count
    }
    '  Input_file
    

    注意:这两种解决方案都会在查找单词中的元音时考虑忽略大小写选项。

    【讨论】:

      【解决方案3】:

      你可以这样做:

      awk '{for(i=1;i<=NF;i++) 
               if($i~/[aA]/ && $i~/[eE]/ && $i~/[iI]/ && $i~/[oO]/ && $i~/[uU]/) 
                    print $I}' file
      

      【讨论】:

        【解决方案4】:

        如果您只想使用 grep 执行此操作,请为每个必需的字母尝试一系列 grep:

        grep -Eo "\w+" myfile.txt | grep -i a | grep -i e | grep -i i | grep -i o | grep -i u
        

        【讨论】:

          【解决方案5】:

          另一个支持多字符 RS 的 awk

          $ awk -v RS=' +|\n|\t' '(w=tolower($0)) ~ /a/ && w ~ /e/ && w ~ /i/ && w ~ /o/ && w ~ /u/' file
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-15
            • 1970-01-01
            • 2020-03-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多