帮助 grep [[:alpha:]]* -o
file.txt 包含:
##w##
##wew##
使用 mac 10.6、bash shell,命令:
cat file.txt | grep [[:alpha:]]* -o
不输出任何内容。我正在尝试提取哈希符号内的文本。我做错了什么?
file.txt contains:
##w##
##wew##
using mac 10.6, bash shell, the command:
cat file.txt | grep [[:alpha:]]* -o
outputs nothing. I'm trying to extract the text inside the hash signs. What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(请注意,在这种情况下,最好将文件名作为参数传递给 grep,而不是通过管道将 cat 的输出传递给 grep:
grep PATTERN file
而不是cat file | grep PATTERN< /code>.)
您使用什么 shell 来执行此命令?我怀疑您的问题是 shell 将星号解释为通配符并尝试 glob 文件。
尝试引用您的模式,例如
grep '[[:alpha:]]*' -o file.txt
。我注意到这与我的 Linux 计算机上的 grep 版本配合得很好,但我的 Mac 上的 grep 需要命令
grep -E '[[:alpha:]]+' -o file.txt< /代码>。
(Note that it is better practice in this instance to pass the filename as an argument to grep instead of piping the output of cat to grep:
grep PATTERN file
instead ofcat file | grep PATTERN
.)What shell are you using to execute this command? I suspect that your problem is that the shell is interpreting the asterisk as a wildcard and trying to glob files.
Try quoting your pattern, e.g.
grep '[[:alpha:]]*' -o file.txt
.I've noticed that this works fine with the version of grep that's on my Linux machine, but the grep on my Mac requires the command
grep -E '[[:alpha:]]+' -o file.txt
.如果你的 bash >3.1
if you have bash >3.1