Вопрос:

Use sed to remove line from json array and comma from previous line

json bash sed

291 просмотра

2 ответа

6 Репутация автора

I've been trying to use sed to remove agent4 as well as the comma after agent 3 (So it stays valid json). I originally tried sed 's/\"agent4\"/ which then morphed into sed 's/,\n\s+\"agent4\"/ but I found out that sed reads individual lines so I haven't been able to progress from here.

It has to use sed.

{
  "environments": {
    "default": {
      "machines": {
        "dev-machine": {
          "agents": [
            "agent1",
            "agent2",
            "agent3",
            "agent4"
          ]
        }
      }
    }
  }
}
Автор: Josh Pinkney Источник Размещён: 08.11.2017 11:46

Ответы (2)


0 плюса

290562 Репутация автора

Using a proper JSON tool like jq:

% jq '.environments.default.machines["dev-machine"].agents |= .[:-1]' tmp.json
{
  "environments": {
    "default": {
      "machines": {
        "dev-machine": {
          "agents": [
            "agent1",
            "agent2",
            "agent3"
          ]
        }
      }
    }
  }
}
Автор: chepner Размещён: 08.11.2017 11:55

0 плюса

638 Репутация автора

Try this command,

$ sed '/3\"/ s/,//g; N; s/\"agent4\"/,/' data.json

Reference: How can I use sed to replace a multi-line string?

Автор: Lucas Ramage Размещён: 09.11.2017 12:34
Вопросы из категории :
32x32