如何使用awk计算包含连续元音的单词? [英] How do I count words that contain consecutive vowels using awk?

查看:87
本文介绍了如何使用awk计算包含连续元音的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全承认这是学校的作业.我不知道从哪里开始寻找解决方案的资源,因为awk的指南似乎很有限.我可以找到用awk进行地址计数的指南,但是找不到如何完成分配参数的指南.有人可以向我指出一些易于理解的初学者资源,或者提供一些有关如何启动它的提示吗?请注意,我确实查看了以下内容:如何仅查找包含两个连续元音的行&我更想了解如何获得答案.

Fully admit that this is an assignment from school. I don't know where to start with a resource for a solution since there seems to be limited guides for awk. I can find guides that address counting in awk, but not how to fulfill the parameters of the assignment. Can someone point me at some easy to understand beginner's resources or provide some hints on how to start this? Note that I did take a look at this: How to find only the lines that contain two consecutive vowels & I'd more like to understand how to obtain the answer.

分配问题-使用Awk:计算包含连续元音(a,e,i,o或u)的单词数,然后显示计数. 连续元音"意思是单词连续包含两个元音,即"look".和好"包含连续的元音,而妈妈"包含连续的元音.才不是.不要显示文件的任何行-输出应该只是计数.

推荐答案

由于您尚未发布任何Input_file,因此我按如下所示创建了自己的测试/示例Input_file,您可以以此为起点.

Since you have NOT posted any Input_file, so I have created my own test/sample Input_file as follows, you could take it as a starting point.

假设您的Input_file如下.

Let's say your Input_file is as follows.

cat Input_file
look look good good  mama mama mama mama mama mama look look look mama
mama mama mama mama mama good good look mama mama mama mama mama mama good look

然后的解决方案是:

awk '
{
  for(i=1;i<=NF;i++){
    if(match($i,/[aeiouAEIOU]{2}/)){ count++ }
  }
  print "Line " FNR " has " count " number of consecutive vowels found."
  count=""
}
' Input_file

或者如果您的awk是旧版本,则可以使用2次[aeiouAEIOU]来避免问题(实际上[aeiouAEIOU]{2}[aeiouAEIOU][aeiouAEIOU]的快捷方式),考虑到您可能还有其他要求,也使用了match函数(例如,在以下说明中提到的->打印索引值,以防您不需要它,然后使用简单条件).

OR in case your awk is old version then you could use 2 times [aeiouAEIOU] to avoid issues(actually [aeiouAEIOU]{2} is shortcut of [aeiouAEIOU][aeiouAEIOU]), also match function was used considering that you may have other requirements(eg--> printing index values, mentioned in following explanation, in case you don't need it then use simple condition).

awk '
{
  for(i=1;i<=NF;i++){
    if(match($i,/[aeiouAEIOU][aeiouAEIOU]/)){ count++ }
  }
  print "Line " FNR " has " count " number of consecutive vowels found."
  count=""
}
' Input_file

详细说明: 添加以上详细说明.

Detailed explanation: Adding detailed explanation for above.

awk '
##Starting awk program from here.
{
  for(i=1;i<=NF;i++){
##Starting a for loop from 1 to till value of NF(number of fields in current line) here.
##In awk by default lines are separated by spaces and NF will have their total number of fields.
    if(match($i,/[aeiouAEIOU]{2}/)){ count++ }
##Checking condiiton if current field is having 2 consecutive vowels then add count variable with 1 here.
##Why I used match here, in case OP needs to have whatever strings are present with its value, OP could make use of
##substr($i,RSTART,RLENGTH) as an index into an array and then could get those values also.
  }
  print "Line " FNR " has " count " number of consecutive vowels found."
##Printing output that current line has how many consecutive vowels into it.
  count=""
##Nullifying count here.
}
' Input_file  ##Mentioning Input_file name here.

输出如下.

Line 1 has 7 number of consecutive vowels found.
Line 2 has 5 number of consecutive vowels found.

这篇关于如何使用awk计算包含连续元音的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆