LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   translate cfg text file to json and json2text (https://www.linuxquestions.org/questions/linux-newbie-8/translate-cfg-text-file-to-json-and-json2text-4175434661/)

vrusu 10-29-2012 03:08 PM

translate cfg text file to json and json2text
 
hi
I have a config file like this one:

[sectionOne]
key1_1=value1_1
key1_n=value1_n
#this is a comment
[sectionTwo]
key2_1=value2_1
key2_n=value2_n

;this is a comment also
[SectionThree]
key3_1=value3_1
key3_n=value3_n

[SectionFor]
....

I need to translate this into json (and reverse), using minimal shell tools (no perl,python,php, just sed,awk available)
Code:

[
    {
        "sectionOne": {
            "key1_1": "value1_1",
            "key1_n": "value1_n"
        }
    },
    {
        "sectionTwo": {
            "key2_1": "value2_1",
            "key2_n": "value2_n"
        }
    },
    {
        "sectionThree": {
            "key3_1": "value3_1",
            "key3_n": "value3_n"
        }
    }
]

Is it possible?
Any idea would be greatly appreciated
thank you in advance!

Didier Spaier 10-29-2012 03:38 PM

It seems that to determine the json lines to output from a line in the input you need to consider what are the contents of the previous (or none) and next (or none) line(s) of that input line.

This can be done with awk.


All times are GMT -5. The time now is 01:04 AM.