I tried to convert it to the JSON format by -J flag, but older lscpu versions have no such functionality.
The solutions I've found here didn't help me.
I want to put out here the solution I got from member of another community.
About solution from @palmasd1
LANG=C
lscpu |
sed -r 's/$ //g;s/ [ ]+//g' |
awk -F ':' 'BEGIN{printf"{\"cpu\":["}{print x"{\""$1"\":\""$2"\"}";x=","}END{printf"]}"}' |
tr -d '\n'
Explanations:
LANG=C: translation deactivationlscpu: the commandawk -F ';' ...: use : character as input field separator BEGIN {
printf "{\"cpu\":["
}
{
print x "{\""$1"\":\""$2"\"}"
x=","
}
END {
printf "]}"
}
BEGIN { ... }: executed before all line treatments{ ... }: treatment for each line
x variable is used for adding a coma before each line except first where x is empty$1 and $2 contains first and second field valuesEND { ... }: executed after all line treatmentstr -d '\n': remove all LINE FEED character (new line under *UNIX* systems)Personal notes about this script:
lscpu -J command/, "...) and field value who contains : characterLANG=C
lscpu | sed -r 's/$ //g;s/ [ ]+//g' |
awk -F ':' 'BEGIN{printf"{\"cpu\":["}{print x"{\""$1"\":\""$2"\"}";x=","}END{printf"]}"}' |
tr -d '\n'
Explanation
lscpu is fed to sed which strips out all spaces at the front of a line, and all double spaces are replaced by single spaces.awk reformats each line into JSON by wrapping the label and the data in double quotes. Also wraps each line in curly braces, and prefixes every line except the first with a comma.tr deletes all the new lines, making one single line of output.This can be "pretty printed" by piping the output to jq