lscpu出力をJSON形式に変換するには?

lscpu出力をJSON形式に変換するには?

lscpuデータをJSON形式で出力する必要がありますが、-Jキーがサポートしていない状況を処理する必要がありました。

そこで、一般的な出力をJSONに変換できる簡単なコマンドラインスクリプトを作成し、ここに答えとして投稿します。

答え1

echo {$(lscpu | sed 's/  */ /g' | sed 's/: /:/g' | awk -F ':' '{print "\x22"$1"\x22: \x22"$2"\x22,"}') | sed 's/,$/}/'

2番目はsed余分なスペースを削除し、awkフィールドの周りに二重引用符を作成し、最後のsedカンマを最初の中括弧で生成された配列を囲む中括弧で置き換えます。

コンソールログ:

lscpu

Architecture:                    aarch64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
CPU(s):                          4
On-line CPU(s) list:             0-3
Thread(s) per core:              1
Core(s) per socket:              4
Socket(s):                       1
Vendor ID:                       ARM
Model:                           3
Model name:                      Cortex-A72
Stepping:                        r0p3
CPU max MHz:                     1800.0000
CPU min MHz:                     600.0000
BogoMIPS:                        108.00
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Spec store bypass: Vulnerable
Vulnerability Spectre v1:        Mitigation; __user pointer sanitization
Vulnerability Spectre v2:        Vulnerable
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           fp asimd evtstrm crc32 cpuid

-----------------------------------------------------------

echo {$(lscpu | sed s/ automation/ snap/ /g | sed s/: /:/g | awk -F : {print "": "",}) | sed s/,$/}/

{
  "Architecture": "aarch64",
  "CPU op-mode(s)": "32-bit, 64-bit",
  "Byte Order": "Little Endian",
  "CPU(s)": "4",
  "On-line CPU(s) list": "0-3",
  "Thread(s) per core": "1",
  "Core(s) per socket": "4",
  "Socket(s)": "1",
  "Vendor ID": "ARM",
  "Model": "3",
  "Model name": "Cortex-A72",
  "Stepping": "r0p3",
  "CPU max MHz": "1800.0000",
  "CPU min MHz": "600.0000",
  "BogoMIPS": "108.00",
  "Vulnerability Itlb multihit": "Not affected",
  "Vulnerability L1tf": "Not affected",
  "Vulnerability Mds": "Not affected",
  "Vulnerability Meltdown": "Not affected",
  "Vulnerability Mmio stale data": "Not affected",
  "Vulnerability Spec store bypass": "Vulnerable",
  "Vulnerability Spectre v1": "Mitigation; __user pointer sanitization",
  "Vulnerability Spectre v2": "Vulnerable",
  "Vulnerability Srbds": "Not affected",
  "Vulnerability Tsx async abort": "Not affected",
  "Flags": "fp asimd evtstrm crc32 cpuid"
}

関連情報