首页 > 解决方案 > 删除长度少于两个字符且不包含元音的单词

问题描述

sed -e 's/ [a-zA-Z0-9]\{1\} / /g' 尽管我不确定如何在一个命令中仅删除不包含元音且长度小于 2 的单词,但您可以删除长度小于 2 的 单词。

因此一个句子 this is my w example of a sentence p 会像这样结束 this is my example of a sentence

标签: awksed

解决方案


请您尝试以下操作。

awk '
{
  val=""
  for(i=1;i<=NF;i++){
    if($i!~/[aieou]/ && length($i)<2){ a="" }
    else{ val=(val?val OFS:"")$i            }
  }
  print val
}
' Input_file

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

awk '                                             ##Starting an awk program from here.
{
  val=""                                          ##Nullifying val value here.
  for(i=1;i<=NF;i++){                             ##Starting a for loop from here.
    if($i!~/[aieou]/ && length($i)<2){ a="" }     ##Checking condition if field is NOT containing any vowels and length is lesser than 2 then do nothing.
    else{ val=(val?val OFS:"")$i            }     ##Else(in case above condition is FALSE) create val which contains current field value.
  }
  print val                                       ##Printing val here.
}
' Input_file                                      ##Mentioning Input_file name here.

推荐阅读