我不得不处理这样的情况:我需要lscpu
输出的 JSON 格式数据,但-J
key 不支持。
所以我制作了简单的命令行脚本,允许将普通输出转换为 JSON,我将在此处发布它作为答案。
答案1
echo {$(lscpu | sed 's/ */ /g' | sed 's/: /:/g' | awk -F ':' '{print "\x22"$1"\x22: \x22"$2"\x22,"}') | sed 's/,$/}/'
两个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"
}