ʞɔı*_*ɔıu 6 regex unix command-line grep
我想获得BSD词典单词列表中每个单词的前两个字母,不包括那些只以一个字母开头的单词.
没有单字母排除,它运行速度非常快:
time cat /usr/share/dict/web2 | cut -c 1-2 | tr '[a-z]' '[A-Z]' | uniq -c > /dev/null
real 0m0.227s
user 0m0.375s
sys 0m0.021s
Run Code Online (Sandbox Code Playgroud)
..然而,在' ' 上gre is很痛苦:
time cat /usr/share/dict/web2 | cut -c 1-2 | grep '..' | tr '[a-z]' '[A-Z]' | uniq -c > /dev/null
real 1m16.319s
user 1m0.694s
sys 0m10.225s
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
在Mac上真正慢的是UTF-8语言环境.
替换为grep ..,LC_ALL=C grep ..然后您的命令将运行超过100倍.
对于Linux来说也是如此,除了给定的Linux发行版可能更可能默认为C环境.