【问题标题】:How to find and replace json with shell variables using jq?如何使用 jq 用 shell 变量查找和替换 json?
【发布时间】:2019-09-10 05:53:50
【问题描述】:

我已经用 jq 从 json 对象中读取了属性并将它们存储到变量中。

我现在想读取这些变量,并从本质上找到字符串中的一个单词并将其替换为一个全局 shell 变量。

我已经从我的 JSON 文件中设置了我的 json ID

# Set Json ID's
TARGET_ID=$(jq '.DefaultCacheBehavior.TargetOriginId' distconfig.json)
DOMAIN_NAME=$(jq '.Origins.Items[0].DomainName' distconfig.json)
ORIGIN_ID=$(jq '.Origins.Items[0].Id' distconfig.json)

echo "$TARGET_ID"
echo "$DOMAIN_NAME"
echo "$ORIGIN_ID"

这会返回

"S3-Website-stag4.example.io.s3-website.us-east-2.amazonaws.com"
"stag4.example.io.s3-website.us-east-2.amazonaws.com"
"S3-Website-stag4.example.io.s3-website.us-east-2.amazonaws.com"

我有我的位置 ID 变量,并想编写它来查找和替换这 3 个 ID 中的所有 stag4 引用。

然后我想将这 3 个 ID 写入初始 json 对象,或者创建它的临时版本。

例如,如果: $DOMAIN_NAME"stag4.example.io.s3-website.us-east-2.amazonaws.com"

我想基本上将其设置为:

$LOCATION_NAME="stag6"
DOMAIN_LOCATION="example.io"
"$DOMAIN_NAME=S3-Website-\$LOCATION_NAME\.example.io.s3-website.us-east-2.amazonaws.com"
"$TARGET_ID=\$LOCATION_NAME\.example.io.s3-website.us-east-2.amazonaws.com"
"$ORIGIN_ID=S3-Website-\$LOCATION_NAME\.example.io.s3-website.us-east-2.amazonaws.com"

然后将这 3 个写入临时或新的 json 文件,以便我可以运行我的 cloudformation 命令:

aws cloudfront create-distribution --distribution-config file://disttemp.json

我现在已经从初始 json 文件中构建了正确的变量,如下所示:

$LOCATION_NAME="stag6"
DOMAIN_LOCATION="example.io"
echo "Build New IDs"
TARGET_ID_BUILT="S3-Website-$LOCATION_NAME.$DOMAIN_LOCATION.s3-website.us-east-2.amazonaws.com"
DOMAIN_NAME_BUILT="$LOCATION_NAME.$DOMAIN_LOCATION.s3-website.us-east-2.amazonaws.com"
ORIGIN_ID_BUILT="S3-Website-$LOCATION_NAME.$DOMAIN_LOCATION.s3-website.us-east-2.amazonaws.com"

echo "$TARGET_ID_BUILT"
echo "$DOMAIN_NAME_BUILT"
echo "$ORIGIN_ID_BUILT"

如何用jq将这些变量写入json文件?

编辑:请求的 distconfig.json 示例 - 域/信用交换到示例

{
  "CallerReference": "my-test-distribution-2",
  "Comment": "", 
  "CacheBehaviors": {
      "Quantity": 0
  }, 
  "IsIPV6Enabled": true, 
  "Logging": {
      "Bucket": "", 
      "Prefix": "", 
      "Enabled": false, 
      "IncludeCookies": false
  }, 
  "WebACLId": "", 
  "Origins": {
      "Items": [
          {
              "OriginPath": "", 
              "CustomOriginConfig": {
                  "OriginSslProtocols": {
                      "Items": [
                          "TLSv1", 
                          "TLSv1.1", 
                          "TLSv1.2"
                      ], 
                      "Quantity": 3
                  }, 
                  "OriginProtocolPolicy": "http-only", 
                  "OriginReadTimeout": 30, 
                  "HTTPPort": 80, 
                  "HTTPSPort": 443, 
                  "OriginKeepaliveTimeout": 5
              }, 
              "CustomHeaders": {
                  "Quantity": 0
              }, 
              "Id": "S3-Website-stag4.example.io.s3-website.us-east-2.amazonaws.com", 
              "DomainName": "stag4.example.io.s3-website.us-east-2.amazonaws.com"
          }
      ], 
      "Quantity": 1
  },
}
"DefaultRootObject": "", 
  "PriceClass": "PriceClass_All", 
  "Enabled": true, 
  "DefaultCacheBehavior": {
      "TrustedSigners": {
          "Enabled": false, 
          "Quantity": 0
      }, 
      "LambdaFunctionAssociations": {
          "Quantity": 0
      }, 
      "TargetOriginId": "S3-Website-stag4.example.io.s3-website.us-east-2.amazonaws.com", 
      "ViewerProtocolPolicy": "redirect-to-https", 
      "ForwardedValues": {
          "Headers": {
              "Quantity": 0
          }, 
          "Cookies": {
              "Forward": "none"
          }, 
          "QueryStringCacheKeys": {
              "Quantity": 0
          }, 
          "QueryString": false
      }, 
      "MaxTTL": 31536000, 
      "SmoothStreaming": false, 
      "DefaultTTL": 86400, 
      "AllowedMethods": {
          "Items": [
              "HEAD", 
              "GET"
          ], 
          "CachedMethods": {
              "Items": [
                  "HEAD", 
                  "GET"
              ], 
              "Quantity": 2
          }, 
          "Quantity": 2
      }, 
      "MinTTL": 0, 
      "Compress": true
  }, 
  "ViewerCertificate": {
      "SSLSupportMethod": "sni-only", 
      "ACMCertificateArn": "xxxx", 
      "MinimumProtocolVersion": "TLSv1.1_2016", 
      "Certificate": "xxxx", 
      "CertificateSource": "acm"
  }, 
  "CustomErrorResponses": {
      "Quantity": 0
  }, 
  "HttpVersion": "http2", 
  "Restrictions": {
      "GeoRestriction": {
          "RestrictionType": "none", 
          "Quantity": 0
      }
  }, 
  "Aliases": {
      "Quantity": 0
  }
}

【问题讨论】:

  • 这个链接可能对stackoverflow.com/questions/42716734/…有帮助
  • 是的,我正在尝试使用建议的临时文件,我只是不确定如何执行“查找和替换”操作,即在新文件中设置新变量来代替我得到的位置初始文件中的旧变量
  • @oguzismail 添加了一个 sn-p,谢谢
  • @user3648969 见How to create a Minimal, Complete, and Verifiable example。您提供的示例是一个不完整的 JSON 值,并且它没有名为 DefaultCacheBehavior 的键,而您的代码表明 TARGET_ID 是从那里检索到的。
  • @oguzismail 抱歉,我在对象底部放了一个省略号以表示它继续,我只是不想用完整的对象把帖子弄得乱七八糟。只要我知道查找和替换变量之一的函数,我就可以将其应用于不同的路径

标签: json shell sh jq


【解决方案1】:

您应该使用sed 进行替换,然后将值注入回JSON。

echo $TARGET_ID | sed 's/stag4/stag5/g'

输出 S3-Website-stag5.example.io.s3-website.us-east-2.amazonaws.com

接下来我们将值放回原始 JSON,这将在技术上输出一个新的 JSON,并且不会编辑文件,但是,您可以通过临时保存到 tmp 文件来轻松解决输出问题。

我们将使用 --arg 标志来引用我们的 bash 变量并为我们的字段设置新值

cat distconfig.json | jq --arg  TARGET_ID $TARGET_ID '.DefaultCacheBehavior.TargetOriginId = $TARGET_ID' > tmp.json && mv tmp.json distconfig.json

【讨论】:

    猜你喜欢
    • 2016-01-31
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 2016-10-06
    相关资源
    最近更新 更多