0

I have multiple files that contain string like this:

result = [
    {
    key1 = value1
    key2 = value2
    key3 = {
           subkey3 = subvalue3
           }

    },
    {
    key3 = value3
    key4 = value4
    }
]

I want to merge those file become 1 file. so here what I did.

  1. get all value inside result usingsed -i '/result\|\]\|\[/d' file

  2. I append the output to a file but inside result = []

    result = [ ]

but I don't know how, and I need to make sure there is comma , separated between each output from file.

4
  • Bash is probably the wrong tool for the job. If you remove the grouping, you will get a key name collision (example "key3"). Commented Sep 9, 2021 at 8:18
  • @Fixlensmith : It is not clear to me from your description, how the output file is supposed to look. Finally, it is not clear how the keys and value look like. After all you are going to write a parser, so unless you define exactly the syntax of your data structure, you can't parse it. Commented Sep 9, 2021 at 8:35
  • @user1934428 the output should look like my example above.. as i mention before, I have multiple file like that, and I want to merge to 1 file. Commented Sep 9, 2021 at 9:14
  • I see only that you have posted the input, and I don't see any definition of the syntax of the items. Commented Sep 9, 2021 at 9:45

1 Answer 1

1

If you want a list of json objects something like this can be done

for file in ./file*; do sed 's/result\ =\ \[/{/' $file | sed 's/\]/},/'; done > output.file 

This would result in an output which somewhat looks like this-

{
    {
    key1 = value1
    key2 = value2
    key3 = {
           subkey3 = subvalue3
           }

    },
    {
    key3 = value3
    key4 = value4
    }
},
{
    {
    key1 = value1
    key2 = value2
    key3 = {
           subkey3 = subvalue3
           }

    },
    {
    key3 = value3
    key4 = value4
    }
},

To convert this to proper json-

  1. Add a "[" to the beginning of the output file
  2. at the end of the output file, replace the very last "," a "]" character.

Hopefully, This solves what you're trying to get done.

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

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.