What is the most efficient case-insensitive grep usage?

蹲街弑〆低调 提交于 2019-12-04 15:00:45

问题


My objective is to match email addresses that belong to the Yahoo! family of domains. In *nix systems (I will be using Ubuntu), what are the benefits and drawbacks to any one of these methods for matching the pattern?

And if there is another, more elegant solution that I haven't been capable of imagining, please share.

Here they are:

  • Use grep with option -i:

grep -Ei "@(yahoo|(y|rocket)mail|geocities)\.com"

  • Translate characters to all upper case or lower case then grep:

tr [:upper:] [:lower:] < /path/to/file.txt | grep -E "@(yahoo|(y|rocket)mail|geocities)\.com"

  • Include a character set for each character in the pattern (the below would of course not match something like "@rOcketmail.com", but you get the idea of what it would become if I checked each character for case):

grep -E "@([yY]ahoo|([yY]|[rR]ocket)[mM]ail|[gG]eo[cC]ities)\.[cC][oO][mM]" /path/to/file.txt


回答1:


grep -i turned out to be significantly slower than translating to lowers before grepping, so I ended up using a variation of #2.

Thanks @mike-w for reminding me that a simple test goes a long way.



来源:https://stackoverflow.com/questions/22924548/what-is-the-most-efficient-case-insensitive-grep-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!