Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Grep信息来自/proc/cpuinfo_Regex_Linux_Ubuntu_Grep_Pattern Matching - Fatal编程技术网

Regex Grep信息来自/proc/cpuinfo

Regex Grep信息来自/proc/cpuinfo,regex,linux,ubuntu,grep,pattern-matching,Regex,Linux,Ubuntu,Grep,Pattern Matching,我试图使用以下命令从Ubuntu中的/proc/cpuinfo grep模型: cat /proc/cpuinfo | grep 'model' 但我得到了两行作为输出: model : 60 model name : Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz model : 60 我将仅获得这一行作为输出: model : 60 model name : Intel(R) Core(TM) i7-4770 CP

我试图使用以下命令从Ubuntu中的/proc/cpuinfo grep模型:

cat /proc/cpuinfo | grep 'model'
但我得到了两行作为输出:

model       : 60
model name  : Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
model       : 60
我将仅获得这一行作为输出:

model       : 60
model name  : Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
model       : 60

如何执行此操作?

使用以下命令验证服务器的型号:

dmidecode | grep -A3 '^System Information'
对我来说,它返回:

System Information
        Manufacturer: IBM
        Product Name: System x3650 M3 -[7945J4A]-
        Version: 00

要仅获取第一行,只需使用head:

如果你想要第n行,你可以同时使用:

cat /proc/cpuinfo | grep 'model' | head -n | tail -1

您可以使用“sed”解析出单独的行

cat/proc/cpuinfo | grep‘model’| sed-n 1p将为您提供型号:60


cat/proc/cpuinfo | grep'model'| sed-n 2p将为您提供第二行作为输出。

只需打印包含最后一个字段为数字的model的行:

awk '/model/ && $NF~/^[0-9]*$/ {print $NF}' /proc/cpuinfo
或者,您甚至可以提供要匹配的模式:

只需使用GNU sed:

这将grep/proc/cpuinfo中的第一行匹配模型并将其打印出来。然后它将终止。

您可以使用grep的-invert match选项删除包含名称的行

  -v, --invert-match
          Invert the sense of matching, to select non-matching lines.
通过使用-v命令变成

 cat /proc/cpuinfo | grep 'model' | grep -v 'name'
cat/proc/cpuinfo|grep'model\s\+:'

此greps表示模型后跟至少1个空格和冒号。

另一种方法:

lscpu | grep Model:
看起来很简单

您只需要:

grep '^model[[:blank:]]*:' /proc/cpuinfo
或者,如果您更喜欢awk:

awk '/^model[[:blank:]]*:/' /proc/cpuinfo
然后,如果您只需要行末尾的数字,那么这只是一个调整:

awk '/^model[[:blank:]]*:/{ print $NF }' /proc/cpuinfo

凯蒂·格雷普·塞德似乎有点矫枉过正fragile@fedorquiIt可能是在用于大规模抓取的脚本中使用此操作时。这似乎是一个快速的解决方法。这是一个处理这个简单问题的疯狂方法。idk如果今晚有满月,但你有一大堆疯狂的答案。忽略任何建议使用cat/proc/cpuinfo |或多个管道的内容,以及任何看起来非常重要的内容。为什么要使用cat?grep“^model\s\+:”/proc/cpuinfo
awk '/^model[[:blank:]]*:/' /proc/cpuinfo
awk '/^model[[:blank:]]*:/{ print $NF }' /proc/cpuinfo