0

I need to generate JSON output from my shell script. I need to get Ram slot details of a particular machine and generate JSON using those details. To get Ram details I am using system_profiler SPMemoryDataType It produces details as follows.

    BANK 0/DIMM0:

      Size: 2 GB
      Type: DDR3
      Speed: 1600 MHz
      Status: OK
      Manufacturer: 0x802C
      Part Number: 0x384A54463235363634485A2D3147364D3120
      Serial Number: 0xE98388E6

    BANK 1/DIMM0:

      Size: 2 GB
      Type: DDR3
      Speed: 1600 MHz
      Status: OK
      Manufacturer: 0x802C
      Part Number: 0x384A54463235363634485A2D3147364D3120
      Serial Number: 0xE98388E5

From that I should form JSON like this

[
{"Bank":"0/DIMM0","Serial Number":"0xE98388E6","Status":"OK"},
{"Bank":"1/DIMM0","Serial Number":"0xE98388E5","Status":"OK"}
]

To extract separate details like bank, Serial Number, Status we can use

system_profiler SPMemoryDataType | awk '/Bank/
system_profiler SPMemoryDataType | awk '/Serial/
system_profiler SPMemoryDataType | awk '/Status/

I am sure that there is a need of Dynamic variable to do form json from the results. But since I am new to shell script I am confused. Is there any way to generate JSON from the output?

2 Answers 2

3
#!/usr/bin/awk -f

$1 == "BANK" {
    bank = $2
    sub(/:/, "", bank)
    while (getline > 0) {
        if ($1 == "Serial" && $2 == "Number:") {
            serial_number = $3
        } else if ($1 == "Status:") {
            status = $2
        }
        if (serial_number != "" && status != "") {
            entries[++e] = "{\"Bank\":\"" bank "\",\"Serial Number\":\"" serial_number "\",\"Status\":\"" status "\"}"
            break
        }
    }
    bank = serial_number = status = ""
}

END {
    print "["
    if (e > 0) {
        printf "%s", entries[1]
        for (i = 2; i <= e; ++i) {
            printf ",\n%s", entries[i]
        }
        print ""
    }
    print "]"
}

Usage:

awk -f script.awk file
system_profiler SPMemoryDataType | awk -f script.awk

Example output:

[
{"Bank":"0/DIMM0","Serial Number":"0xE98388E6","Status":"OK"},
{"Bank":"1/DIMM0","Serial Number":"0xE98388E5","Status":"OK"}
]

Using within a shell script:

#!/bin/bash

system_profiler SPMemoryDataType | awk '$1 == "BANK" {
    bank = $2
    sub(/:/, "", bank)
    while (getline > 0) {
        if ($1 == "Serial" && $2 == "Number:") {
            serial_number = $3
        } else if ($1 == "Status:") {
            status = $2
        }
        if (serial_number != "" && status != "") {
            entries[++e] = "{\"Bank\":\"" bank "\",\"Serial Number\":\"" serial_number "\",\"Status\":\"" status "\"}"
            break
        }
    }
    bank = serial_number = status = ""
}

END {
    print "["
    if (e > 0) {
        printf "%s", entries[1]
        for (i = 2; i <= e; ++i) {
            printf ",\n%s", entries[i]
        }
        print ""
    }
    print "]"
}'

A one-liner:

system_profiler SPMemoryDataType | awk '$1=="BANK"{bank=$2;sub(/:/,"",bank);while(getline>0){if($1=="Serial"&&$2=="Number:"){serial_number=$3}else if($1=="Status:"){status=$2};if(serial_number!=""&&status!=""){entries[++e]="{\"Bank\":\""bank"\",\"SerialNumber\":\""serial_number"\",\"Status\":\""status"\"}";break}};bank=serial_number=status=""}END{print "[";if(e>0){printf "%s",entries[1];for(i=2;i<=e;++i){printf ",\n%s",entries[i]};print""};print "]"}'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your response. I am working on a Remote Mac Machine from a Windows system.I am using plink to execute shell script which is in a local machine, I mean Windows. So If I use the script given by yours I need to copy the script.awk to the remote machine which might make the process slower. So is there any possibility to have this script with default single #! /bin/bash file?
@CMb Yes you can embed it on a shell script. Please see update.
0

There are some few libraries to do so.. One such thing is https://github.com/jeganathgt/libjson-sh . It is standalone shell script library, provides easy handy API's to generate json output in console.

Ex :

json_init
json_add_string "serial"  "$<cmd>"
json_add_string "bankinfo"   "$<cmd>"
json_add_string "status" "$<cmd>"
json_dump

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.