2

I have a shell script version.sh in which i have code like.

#!/bin/sh
data = `lsb_release -a`
echo $data

it returned me the output like:

Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal

so what I exactly want is want a JSON file to be created .and data should be there like

{'Release' : 20.04 , 'Codename' : focal}

I want to print data in JSON form .can anyone please help me related this ?? I am stuck here.any suggestion any help would be matter.

2
  • 1
    Perhaps an easier way would be to write a small awk script. You could pipe the output from lsb_release into awk, collect the values for release and codename, and in the END clause of awk you can generate the JSON string. Commented Apr 21, 2022 at 7:49
  • BTW, why is this tagged bash? It seems to be a sh-script. May I suggest to remove the bash-tag? Commented Apr 21, 2022 at 7:50

2 Answers 2

3

You can use python ttp module to get this data. See the following example:

from ttp import ttp
import json

data_to_parse = """
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal
"""

ttp_template = """
Release:    {{release}}
Codename:   {{codename}}

"""

parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()

# print result in JSON format
results = parser.result(format='json')[0]
#print(results)

#converting str to json. 
result = json.loads(results)

print(result)

See the output:

enter image description here

EDITED as requested (without using any package):

data_to_parse = """
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal
"""

lines = data_to_parse.splitlines()

result = {}

for line in lines:
    if 'Release:' in line:
        line2 = line.split(':')
        result['Release'] = (line2[1].strip())

    elif 'Codename:' in line:
        line2 = line.split(':')
        result['Codename'] = (line2[1].strip())

print(result)

See the result:

enter image description here

EDITED 2. time:

New Data:

data_to_parse = """
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal

Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    22.04
Codename:   local

Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    21.04
Codename:   cocal

"""

The code:

lines = data_to_parse.splitlines()

result_list = []
result2 = {}
result_list3 = []

for line in lines:
    
    if 'Release:' in line:
        line2 = line.split(':')
        result_list.append({'Release': line2[1].strip()})
    
    elif 'Codename:' in line:
        line2 = line.split(':')
        result_list.append({'Codename': line2[1].strip()})
        result2 = {**result_list[0], **result_list[1]}
        result_list3.append(result2)
        result_list = []

print(result_list3)

The output:

enter image description here

Sign up to request clarification or add additional context in comments.

10 Comments

Hello Baris Ozensel, Can't we do that without the ttp package ??
Hi Neha, there should be some other ways to do that. As per my experience, ttp is the most easiest one.
I was asking because I can not install any package .so if there would be any solution without installing the package it would be great and helpful for me.
Hey thanx Baris Ozensel . that is what i exactly want but one issue is there it is not working with multiple data. when data_to_parse will be in multi data set then it should be return like [{'Release':'20.04', 'focal', '21.04'},{'Release':'20.04', 'focal', '21.04'}]
You can create an empty list and add your data into the list. To do this, create an empty list as following result_list = [] first. Later on, result_list.append(result) needs to be added in one of if or elif statement (not at the same time.) This should work.
|
0

Not tagged or asked but with jq generally being an optimal choice for generating and/or manipulating JSON data in/for the shell (which indeed is tagged, along with bash), here's how a solution using jq could look like:

  • the -R option makes the input lines being interpreted as raw strings
  • the -n option makes the inputs keyword stand for an iterator over all input lines
  • the [inputs | …] array generator creates an array with one item per input line
  • the capture filter applies a regex on an input, and turns the matches into an object
  • the from_entries filter reduces an array of objects with key/value entries into one object
  • the {Release, Codename} object generator creates an object by filtering to only certain fields
lsb_release -a | jq -Rn '
  [ inputs | capture("(?<key>.*):\\s+(?<value>.*)") ]
  | from_entries | {Release, Codename}
'
{
  "Release": "20.04",
  "Codename": "focal"
}

Demo

If desired, adding the -c option would turn the output into a single line:

lsb_release -a | jq -Rnc '…'
{"Release":"20.04","Codename":"focal"}

Demo

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.