I am trying to parse the output of the Linux lscpu
command with Python regex. For example, I am trying to catch the CPU Model name and print it with code below:
import re
import subprocess
cpu_model_name_pattern = re.compile(r's{1,2}Modelsname:s+([a-zA-Z0-9.@s+()-]+)$')
proc = subprocess.run('lscpu', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True, check=True, encoding="utf-8")
print(cpu_model_name_pattern.findall(proc.stdout))
Output:
['Intel(R) Xeon(R) Platinum 8380 CPU @ 2.30GHznCPU family']
Expected CPU model name:
['Intel(R) Xeon(R) Platinum 8380 CPU @ 2.30GHz']
Unlike on the regex101.com
online tester, the above regex keeps matching end of line n
and next line title in lscpu
command output even though there is an explicit $
at the end of my regex.
I have tried to prepend the regex with ^
to match from the beginning of the string, but didn’t help either.
Here are a few lines from lscpu
command output:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 40 bits physical, 57 bits virtual
Byte Order: Little Endian
CPU(s): 12
On-line CPU(s) list: 0-11
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) Platinum 8380 CPU @ 2.30GHz
CPU family: 6
Model: 106