gpt4 book ai didi

linux - Grep 返回 ace 序列而不是我期望的结果

转载 作者:太空狗 更新时间:2023-10-29 11:29:17 26 4
gpt4 key购买 nike

我有一个名为 threads.log 的文件,这是该文件的示例

Threads:    40
Threads: 1
Threads: 1
Threads: 3
Threads: 5
Threads: 5
Threads: 1
Threads: 5
// rest

我在终端中这样做:echo $(cat threads.log | grep -o [0-9]*)

这是输出:1 1 1 1 1 1 1 而不是预期的数字。为什么会这样?

最佳答案

这条线有很多错误(或次优)。

echo $(cat threads.log | grep -o [0-9]*)

包装 echo 完全没用(虽然它做了一些但没什么用),将其删除。

cat threads.log | grep -o [0-9]*

没有必要 cat | grep grep 直接获取文件。

grep -o [0-9]* threads.log

[0-9]* 是一个 shell glob。如果你有,我希望你有,一个在你当前目录中以数字开头的文件,然后 shell 将扩展 [0-9]* 到匹配文件列表和你的 grep 不会得到您期望的参数。引用模式。

grep -o '[0-9]*' threads.log

它可以匹配零位数字并且不产生任何输出。您需要一位或多位数字,因此请使用 \+

grep -o '[0-9]\+' threads.log

关于linux - Grep 返回 ace 序列而不是我期望的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26871734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com