Home > Software design >  apply condition on json file and fetch the value and append the output in bash
apply condition on json file and fetch the value and append the output in bash

Time:07-18

I have a jq which fetches the particular value from the json file and append the output to a text file. Text file contains the [OPTIONS] NAME for Eg: docker-virtual.artifactory.com.

So the output should go and append as a RepositoryName:TAG to the existing one in the text file.

  "dependencies": [
  {
   "name": "powershell-core",
   "type": "zip",
   "path": "Dependencies",
   "filename": "PowerShell-Core-6.2.3-win-x64.msi"
  },
  {
   "name": "redis",
   "type": "docker-image",
   "path": "redis:6.0.10-alpine3.12",
   "filename": ""
  },
  {
  "name": "keycloak",
  "type": "docker-image",
  "path": "jboss/keycloak:12.0.1",
  "filename": ""
  },
  ]

From this Json file I am fetching the value with the type as docker-image and redirecting the values to a file output.txt

jq -r '.dependencies[] | select(.type == "docker-image").path' packages.json > output.txt

So the output.txt file looks like:

redis:6.0.10-alpine3.12
jboss/keycloak:12.0.1

But I am trying to append this output to a file like:

        docker-virtual.artifactory.com/redis:6.0.10-alpine3.12

How to achieve this?

CodePudding user response:

How about:

jq -r '.dependencies[] | select(.type == "docker-image").path' packages.json |  sed 's/.*/docker-virtual.artifactory.com\/&/'> output.txt

Note this will rewrite output.txt file every time, if your only intention is to append to it then use the '>>' sign like so:

jq -r '.dependencies[] | select(.type == "docker-image").path' packages.json |  sed 's/.*/docker-virtual.artifactory.com\/&/'>> output.txt

Hope this helps.

  • Related