【问题标题】:using the right grep option使用正确的 grep 选项
【发布时间】:2020-06-16 12:10:21
【问题描述】:

我需要有关在 shell 脚本中设置正确 grep 命令的帮助。 我目前使用的是:

grep '[A-Za-z]|' words

(我正在使用一个包含大量单词的大型文本文件。它是几本字典的大小。)

问题是我要过滤掉单字母单词(ai&o除外),单词不能少于2个字符,而且必须包含元音。

我不确定如何执行此操作。我假设您使用“或”表达式。我尝试使用但失败的是:

grep '[A-Za-z]|[a,i,o]'

然后我不知道如何强制它匹配 2 个字符。

【问题讨论】:

  • 查看“锚定”您的正则表达式
  • 不要使用 or 除非你愿意只接受它的一侧而不接受另一侧作为有效匹配。
  • 字符类[a,i,o]等价于[aio,];你不需要逗号。
  • 你跑grep --help了吗?您在 Linux 上,因此您将拥有 GNU grep,它支持 --help 选项。您应该查看可用的选项。 -v-i-E-o-w 中的一些可能会有所帮助——可能还有其他的(-P 可能)。当您说“过滤掉单字母单词”时,您的意思是您只想选择 aio 作为单字母单词(不区分大小写匹配),否则接受以下单词两个或多个字符长,并且至少包含一个元音?您想只查看单词还是要查看嵌入单词的行?
  • words 文件的格式是什么?每行一个字?有标点符号要处理吗?数字?空间? “don't”是一个词吗?

标签: linux shell ubuntu grep


【解决方案1】:

我认为以下正则表达式可以完成这项工作:

^[^aio]{1}|[a-zA-Z0-9]{2,}$

【讨论】:

  • 它不起作用我重新尝试了 grep '[A-Za-z]^[^aio]{1}$'
  • 抱歉,试试:^[^aio]{1}|[a-zA-Z0-9]{2,}$
  • 为什么是0-9{2,} 部分如何确保至少有一个元音?
【解决方案2】:

假设您要选择有效词,不区分大小写,其中有效词可以是:

  • 单字母单词“a”、“i”、“o”
  • 以元音开头的两个或多个字母的单词
  • 以元音结尾的两个或多个字母的单词
  • 三个或更多字母的单词,中间有一个元音

那么你可以使用:

grep -Eiwo -e '[aio]|[aeiouy][a-z]+|[a-z]+[aeiouy]|[a-z]+[aeiouy][a-z]+'

选项:

  • -E 扩展正则表达式(使用|+
  • -i不区分大小写
  • -w 匹配单词边界
  • -o 只输出匹配项
  • -e regexp — 要匹配的正则表达式

正则表达式:

  • 4 个替代术语,以| 分隔
  • 选择单字母单词
  • 选择以元音开头并在前导元音后包含一个或多个字母的单词
  • 选择以元音结尾且在尾随元音前包含一个或多个字母的单词
  • 选择以一个或多个字母开头的单词,后跟一个元音,然后是一个或多个字母。

这些模式不是相互排斥的。例如,给定输入词oboe,所有三个多字母模式都匹配它——只有单字母模式失败(毕竟它确实有四个字母)。但是,只有一种模式需要与之匹配;其他人不会更改该单词的输出。

这是一些文字(据说来自“德古拉”):

 "Then he spoke to me mockingly, 'And so you, like the others, would play
your brains against mine. You would help these men to hunt me and
frustrate me in my designs! You know now, and they know in part already,
and will know in full before long, what it is to cross my path. They
should have kept their energies for use closer to home. Whilst they
played wits against me--against me who commanded nations, and intrigued
for them, and fought for them, hundreds of years before they were
born--I was countermining them. And you, their best beloved one, are now
to me, flesh of my flesh; blood of my blood; kin of my kin; my bountiful
wine-press for a while; and shall be later on my companion and my
helper. You shall be avenged in turn; for not one of them but shall
minister to your needs. But as yet you are to be punished for what you
have done. You have aided in thwarting me; now you shall come to my
call. When my brain says "Come!" to you, you shall cross land or sea to
do my bidding; and to that end this!' With that he pulled open his
shirt, and with his long sharp nails opened a vein in his breast. When
the blood began to spurt out, he took my hands in one of his, holding
them tight, and with the other, my neck and pressed my mouth to
the wound, so that I must either suffocate or swallow some of the---- Oh
my God! my God! what have I done? What have I done to deserve such a
fate, I who have tried to walk in meekness and righteousness all my
days. God pity me! Look down on a poor soul in worse than mortal peril;
and in mercy pity those to whom she is dear!" Then she began to rub her
lips as though to cleanse them from pollution.

"Oh, no, not distressed me," she replied, "but I have been more touched
than I can say by your grief. That is a wonderful machine, but it is
cruelly true. It told me, in its very tones, the anguish of your heart.
It was like a soul crying out to Almighty God. No one must hear them
spoken ever again! See, I have tried to be useful. I have copied out the
words on my typewriter, and none other need now hear your heart beat, as
I did." 

这是上面的grep 命令从该文本中选择的排序的、不区分大小写的单词列表,以列格式显示:

a             again         against       aided         all           Almighty      already
And           anguish       are           as            avenged       be            beat
been          before        began         beloved       best          bidding       blood
born          bountiful     brain         brains        breast        but           by
call          can           cleanse       closer        come          commanded     companion
copied        countermining cross         cruelly       crying        days          dear
deserve       designs       did           distressed    do            done          down
either        end           energies      ever          fate          flesh         for
fought        from          frustrate     full          God           grief         hands
have          he            hear          heart         help          helper        her
his           holding       home          hundreds      hunt          I             in
intrigued     is            it            its           kept          kin           know
land          later         like          lips          long          Look          machine
me            meekness      men           mercy         mine          minister      mockingly
more          mortal        mouth         must          my            nails         nations
neck          need          needs         no            none          not           now
of            Oh            on            one           open          opened        or
other         others        out           part          path          peril         pity
play          played        pollution     poor          press         pressed       pulled
punished      replied       righteousness rub           say           says          sea
See           shall         sharp         she           shirt         should        so
some          soul          spoke         spoken        spurt         such          suffocate
swallow       than          that          the           their         them          Then
these         they          this          those         though        thwarting     tight
to            told          tones         took          touched       tried         true
turn          typewriter    use           useful        vein          very          walk
was           were          what          When          while         Whilst        who
whom          will          wine          With          wits          wonderful     words
worse         would         wound         years         yet           you           your

【讨论】:

  • 谢谢你的兄弟,我不知道你是怎么想出来的,但感谢上帝你遇到了我的问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2021-05-23
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多