Júlio Falbo
Júlio Falbo

Reputation: 1926

Add comma after the last curly brackets in shell script

I would like to add a comma , after the last } in a shell script.

From:

[
  {
    "targets": [ "localhost:32804", "localhost:32805" ],
    "labels": {
      "env": "local",
      "job": "instances"
    }
  }
]

To:

[
  {
    "targets": [ "localhost:32804", "localhost:32805" ],
    "labels": {
      "env": "local",
      "job": "instances"
    }
  },
]

Upvotes: 0

Views: 451

Answers (1)

user5683823
user5683823

Reputation:

Here's one way to do this, with sed. Using the -z flag to pretend that lines are terminated by ASCII character 0 (NUL) rather than newline - so that sed sees the entire input file as a single long line.

sed -Ez 's/(})([^}]*$)/\1,\2/' <filename>

Upvotes: 1

Related Questions