bash 帮助 grep [[:alpha:]]* -o
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2430607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
help with grep [[:alpha:]]* -o
提问by nightfire
file.txt contains:
file.txt 包含:
##w##
##wew##
using mac 10.6, bash shell, the command:
使用 mac 10.6,bash shell,命令:
cat file.txt | grep [[:alpha:]]* -o
cat file.txt | grep [[:alpha:]]* -o
outputs nothing. I'm trying to extract the text inside the hash signs. What am i doing wrong?
什么都不输出。我正在尝试提取井号内的文本。我究竟做错了什么?
回答by RTBarnard
(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 fileinstead of cat file | grep PATTERN.)
(请注意,在这种情况下,更好的做法是将文件名作为参数传递给 grep,而不是将 cat 的输出通过管道传递给 grep:grep PATTERN file而不是cat 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.
你用什么shell来执行这个命令?我怀疑您的问题是 shell 将星号解释为通配符并尝试 glob 文件。
Try quoting your pattern, e.g. grep '[[:alpha:]]*' -o file.txt.
尝试引用您的模式,例如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.
我注意到这在我的 Linux 机器上的 grep 版本上运行良好,但我的 Mac 上的 grep 需要命令grep -E '[[:alpha:]]+' -o file.txt.
回答by Vijay
sed 's/#//g' file.txt
/SCRIPTS [31]> cat file.txt
##w##
##wew##
/SCRIPTS [32]> sed 's/#//g' file.txt
w
wew
回答by ghostdog74
if you have bash >3.1
如果你有 bash >3.1
while read -r line
do
case "$line" in
*"#"* )
if [[ $line =~ "^#+(.*)##+$" ]];then
echo ${BASH_REMATCH[1]}
fi
esac
done <"file"

