为什么这个'grep的-o`失败,我应该怎么解决呢? [英] Why does this `grep -o` fail, and how should I work around it?
问题描述
由于输入
echo abc123def | grep -o '[0-9]*'
在一台计算机(与GNU grep的2.5.4),这将返回 123
,并在另一个(与GNU grep的2.5.1),它返回一个空字符串。是否有为什么grep的2.5.1失败在这里,或者是它只是一个错误一些解释?我这样的,我希望能够在不同的计算机(其中可能有不同版本的grep)运行bash脚本,使用的grep -o
。有没有一种正确的方式来获得一致的行为?
On one computer (with GNU grep 2.5.4), this returns 123
, and on another (with GNU grep 2.5.1) it returns the empty string. Is there some explanation for why grep 2.5.1 fails here, or is it just a bug? I'm using grep -o
in this way in a bash script that I'd like to be able to run on different computers (which may have different versions of grep). Is there a "right way" to get consistent behavior?
推荐答案
是的,2.5.1的 -o
的处理是越野车:
<一href=\"http://www.mail-archive.com/bug-grep@gnu.org/msg00993.html\">http://www.mail-archive.com/bug-grep@gnu.org/msg00993.html
Yes, 2.5.1's -o
handling was buggy:
http://www.mail-archive.com/bug-grep@gnu.org/msg00993.html
grep的大概不是这个合适的工具; SED
或 TR
甚至 perl的
可能是更好的视实际的任务是什么。
Grep is probably not the right tool for this; sed
or tr
or even perl
might be better depending on what the actual task is.
这篇关于为什么这个'grep的-o`失败,我应该怎么解决呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!