0

我在变量名中有以下JSON它在containerDefinitions下的末尾TASK_DEFINTIION 有一个\r字符"image": "700707367057.dkr.ecr.us-east-1.amazonaws.com/php-demo:feature-feature01\r"

TASK_DEFINITION_AFTER= 'echo $TASK_DEFINTIION | sed "s/\\r//g"'用来删除\r但似乎它正在删除所有隐藏的回车符,但没有删除作为常规字符可见的回车符。

任何帮助都会受到高度评价。

{
      "memory": "1024",
      "networkMode": "awsvpc",
      "family": "ecs-php-demo",
      "placementConstraints": [],
      "cpu": "512",
      "executionRoleArn": "arn:aws:iam::700707367057:role/ecsTaskExecutionRole",
      "volumes": [],
      "requiresCompatibilities": [
        "FARGATE"
      ],
      "taskRoleArn": "arn:aws:iam::700707367057:role/ecsTaskExecutionRole",
      "containerDefinitions": [
        {
          "memoryReservation": 256,
          "environment": [],
          "name": "ecs-php-demo",
          "mountPoints": [],
          "image": "700707367057.dkr.ecr.us-east-1.amazonaws.com/php-demo:feature-feature01\r",
          "cpu": 0,
          "portMappings": [
            {
              "protocol": "tcp",
              "containerPort": 8080,
              "hostPort": 8080
            }
          ],
          "logConfiguration": {
            "logDriver": "awslogs",
            "options": {
              "awslogs-region": "us-east-1",
              "awslogs-stream-prefix": "ecs",
              "awslogs-group": "/ecs/ecs-php-demo"
            }
          },
          "essential": true,
          "volumesFrom": []
        }
      ]
    }
4

3 回答 3

2

jq rtrimstr用于保持与 JSON 语法一致:

#!/usr/bin/bash

TASK_DEFINTIION="$(
  jq '.containerDefinitions[].image|=rtrimstr("\r")' <<<"$TASK_DEFINTIION"
)"

echo "$TASK_DEFINTIION"

man jq

rtrimstr(str) 如果它以它结尾,则输出它的输入并删除给定的后缀字符串。

      jq ´[.[]|rtrimstr("foo")]´
         ["fo", "foo", "barfoo", "foobar", "foob"]
      => ["fo","","bar","foobar","foob"]
于 2020-05-29T12:12:48.327 回答
1

在您的命令中,第一个 \ 转义了第二个 \,因此sed只看到一个 \

你需要 :

TASK_DEFINITION_AFTER="$(echo $TASK_DEFINTIION | sed "s/\\\\r//g")"
于 2020-05-29T11:30:32.053 回答
1

您可以使用jq替换键值:

jq '.containerDefinitions[].image="700707367057.dkr.ecr.us-east-1.amazonaws.com/php-demo:feature-feature01"' file.json

但不幸的是,jq不支持就地编辑,因此您必须先重定向到临时文件,然后用它替换原始文件,或者使用sponge更多 utils 包中的实用程序,如下所示:

jq '.containerDefinitions[].image="700707367057.dkr.ecr.us-east-1.amazonaws.com/php-demo:feature-feature01"' file.json|sponge file.json

用 删除的纯jq解决方案:\rgsub

jq '.containerDefinitions[].image|=gsub("[\r]"; "")' file.json|sponge file.json

输出样本:

{
  "memory": "1024",
  "networkMode": "awsvpc",
  "family": "ecs-php-demo",
  "placementConstraints": [],
  "cpu": "512",
  "executionRoleArn": "arn:aws:iam::700707367057:role/ecsTaskExecutionRole",
  "volumes": [],
  "requiresCompatibilities": [
    "FARGATE"
  ],
  "taskRoleArn": "arn:aws:iam::700707367057:role/ecsTaskExecutionRole",
  "containerDefinitions": [
    {
      "memoryReservation": 256,
      "environment": [],
      "name": "ecs-php-demo",
      "mountPoints": [],
      "image": "700707367057.dkr.ecr.us-east-1.amazonaws.com/php-demo:feature-feature01",
      "cpu": 0,
      "portMappings": [
        {
          "protocol": "tcp",
          "containerPort": 8080,
          "hostPort": 8080
        }
      ],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "ecs",
          "awslogs-group": "/ecs/ecs-php-demo"
        }
      },
      "essential": true,
      "volumesFrom": []
    }
  ]
}
于 2020-05-29T14:33:45.927 回答