将 json 中的 \r 替换为 shell
Replace \r from json in shell
我在变量名JSON下面有TASK_DEFINTIION
它在 containerDefinitions
下的 "image": "700707367057.dkr.ecr.us-east-1.amazonaws.com/php-demo:feature-feature01\r"
末尾有一个 \r 字符
我正在使用 TASK_DEFINITION_AFTER= 'echo $TASK_DEFINTIION | sed "s/\r//g"'
删除 \r 但似乎它正在删除所有隐藏的马车 return 但没有删除可见的作为常规字符。
如有任何帮助,我们将不胜感激。
{
"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": []
}
]
}
在你的命令中,第一个 \ 转义了第二个 \,所以 sed
只看到一个 \
你需要:
TASK_DEFINITION_AFTER="$(echo $TASK_DEFINTIION | sed "s/\\r//g")"
使用 jq
rtrimstr
以保持符合 JSON 语法:
#!/usr/bin/bash
TASK_DEFINTIION="$(
jq '.containerDefinitions[].image|=rtrimstr("\r")' <<<"$TASK_DEFINTIION"
)"
echo "$TASK_DEFINTIION"
man jq
:
rtrimstr(str)
Outputs its input with the given suffix string removed, if it ends with it.
jq ´[.[]|rtrimstr("foo")]´
["fo", "foo", "barfoo", "foobar", "foob"]
=> ["fo","","bar","foobar","foob"]
您可以使用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
用 gsub
删除 \r
的纯 jq
解决方案:
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": []
}
]
}
我在变量名JSON下面有TASK_DEFINTIION
它在 containerDefinitions
"image": "700707367057.dkr.ecr.us-east-1.amazonaws.com/php-demo:feature-feature01\r"
末尾有一个 \r 字符
我正在使用 TASK_DEFINITION_AFTER= 'echo $TASK_DEFINTIION | sed "s/\r//g"'
删除 \r 但似乎它正在删除所有隐藏的马车 return 但没有删除可见的作为常规字符。
如有任何帮助,我们将不胜感激。
{
"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": []
}
]
}
在你的命令中,第一个 \ 转义了第二个 \,所以 sed
只看到一个 \
你需要:
TASK_DEFINITION_AFTER="$(echo $TASK_DEFINTIION | sed "s/\\r//g")"
使用 jq
rtrimstr
以保持符合 JSON 语法:
#!/usr/bin/bash
TASK_DEFINTIION="$(
jq '.containerDefinitions[].image|=rtrimstr("\r")' <<<"$TASK_DEFINTIION"
)"
echo "$TASK_DEFINTIION"
man jq
:
rtrimstr(str)
Outputs its input with the given suffix string removed, if it ends with it.jq ´[.[]|rtrimstr("foo")]´ ["fo", "foo", "barfoo", "foobar", "foob"] => ["fo","","bar","foobar","foob"]
您可以使用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
用 gsub
删除 \r
的纯 jq
解决方案:
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": []
}
]
}