Well, for a simple approach, you could just do:
sed 's/\(.*\)\t:\(.*\)/"\1" : "\2"/' /proc/cpuinfo
That will match everything up to a tab followed by a colon and save it as \1
, then everything after the colon and save it as \2
. The replacement puts quotes around them.
That, however, results in cases like these:
"fpu " : " yes"
power management:
Items with extra whitespace before the tab have their whitespace included and empty ones are ignored. This Perl version deals with those correctly:
perl -F: -alpe 's/.*/"$F[0]" : "$F[1]"/' /proc/cpuinfo
This will split the line on :
into the @F
array (-F
sets the character to split on and -a
turns on automatic splitting into @F
) and print each side quoted. It will break if you have more than one :
on a line but I don't think that will ever happen in /proc/cpuinfo
. However, it also prints any blank lines in the file. To avoid that, pipe it through grep
first:
grep . /proc/cpuinfo | perl -F: -alpe 's/.*/"$F[0]" : "$F[1]"/' /proc/cpuinfo
Or, only print if a line contains :
:
perl -F: -alne 's/.*:.*/"$F[0]" : "$F[1]"/ && print' /proc/cpuinfo