Search code examples
jsonbashsedjqauto-generate

Generate file and replace strings in template file in the generated files


I have a JSON file:

{
    "header": {
        "uuid": "c578592a-a751-4993-9060-53f488597e59",
        "timestamp": 1522938800,
        "productionDateTime": "2018-04-05T14:33:20.000+00:00",
        "producers": {
            "operator": null,
            "application": {
                "com.example": {
                    "id": {
                        "string": "1"
                    },
                    "name": {
                        "string": "Test"
                    }
                }
            },
            "station": null,
            "equipment": null,
            "partner": null,
            "flow": null
        },
        "correlationContext": {
            "array": [{
                "correlationId": "98440498-3104-479e-9c99-f4449ba8f4b6",
                "correlationDateTime": {
                    "string": "2018-04-05T14:30:39.000+00:00"
                }
            }]
        }
    },
}

I would like to create n files that copy the JSON file but with a random correlationId and correlationDateTime.

Do you have any tips or suggestions? Many thanks in advance!


Solution

  • Here you go:-

    #!/bin/bash
    alias uid="python -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)' | pbcopy && pbpaste && echo"
    dt=$(date "+%Y%m%d-%H%M%S")
    # For above two values I am leaving with you. If you have best option please go for it
    i=0
    while [ $i -le 20 ]
    do
      sed  "/correlationId/s/98440498-3104-479e-9c99-f4449ba8f4b6/"$uid"/;s/2018-04-05T14:30:39.000+00:00/"$dt"/" yourJsonFile.json > testcase$i.json
      i=$((i+1))
    done