我尝试构建一个标识我的 CPU 的字符串。
cat /proc/cpuinfo | grep "model name"
model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
我想要的是字符串一次和核心数。
Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz - 8 Cores
我怎样才能用 bash/awk/sed 得到这个最好的方法?
否则我会用 perl 来做,但我认为它也应该与管道一起工作。
使用 GNU
awk
:
$ cat /proc/cpuinfo | awk -F': ' '/model name/ {models[$2]++} END {for (i in models) printf "%s - %s\n", i, models[i]}'
Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz - 8 Cores
可以使用上面评论的 uniq 然后传递给 sed :
cat /proc/cpuinfo | grep "model name" \
| uniq -c \
| sed -E 's/[[:space:]]+ ([[:digit:]]+) model name[[:space:]]+: (.+)$/\2 - \1 Cores/g'