0

I have a situation where i have output of one command, like this,

192.168.1.84
192.168.1.85

i want to use this, and make changes in another file, i.e add this ip addresses one after another like in this form. Below the ResourceRecordSet line.

ubuntu@kops:/mujahid$ cat change-resource-record-sets.json
{
    "Comment": "Update record to reflect new IP address of home router",
    "Changes": [
        {
            "Action": "UPSERT",
            "ResourceRecordSet": {
                "Name": "testing.mak.online.",
                "Type": "A",
                "TTL": 60,
                "ResourceRecords": [
                    {
                        "Value": "192.168.1.84"
                    },
                    {
                        "Value": "192.168.1.5"
                    }
                ]
            }
        }
    ]
}
3
  • What should the values for Action, Name, Type, and TTL be in the new Changes array item? It would be better if you added the expected output to the question so that we don't have to guess what you mean.
    – Kusalananda
    Apr 11, 2018 at 5:33
  • all same as above, those are the default for my env. Apr 11, 2018 at 5:34
  • see i want the exact above just want the ip address to be added from the previous cmd. Apr 11, 2018 at 5:36

2 Answers 2

4

To create a proper JSON from the list of IP-addresses generated by somecommand, using jq:

somecommand | jq -Rs '{
  Comment: "Update record to reflect new IP address of home router",
  Changes: [ {
      Action: "UPSERT",
      ResourceRecordSet: {
        Name: "testing.mak.online.",
        Type: "A",
        TTL: 60,
        ResourceRecords: split("\n")|.[0:-1]|map({Value:.})
     } } ] }'

This results in

{
  "Comment": "Update record to reflect new IP address of home router",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "testing.mak.online.",
        "Type": "A",
        "TTL": 60,
        "ResourceRecords": [
          {
            "Value": "192.168.1.84"
          },
          {
            "Value": "192.168.1.85"
          }
        ]
      }
    }
  ]
}

given that somecommand outputs

192.168.1.84
192.168.1.85
6
  • hey! it worked, (partially) it perfectly created a json format,, but my requirement is to add the ip address below the ResourceRecords: which is already in the file, i have re added entire file above.. Apr 11, 2018 at 3:10
  • @Mohd See updated answer.
    – Kusalananda
    Apr 11, 2018 at 6:27
  • i checked with ur provided answer, although it works, but its not completely same..its missing the starting bracket with comment line. Apr 11, 2018 at 10:36
  • @Mohd I think I have been misunderstanding you the whole time. You want to create that JSON document? It does not already exist? I thought you wanted to update it with new IP-addresses... Can you confirm this please, and it will be easy to fix.
    – Kusalananda
    Apr 11, 2018 at 10:41
  • sorrry for misunderstanding, initially i only wanted the ip part, but later u provided a wonderful answer to create the entire file, so now i request u help me create this entire file , which i have pasted in my original questiion. so that i can directly use it to update my records Apr 11, 2018 at 10:55
-2

You could store your input in a array and output everything with looping over it.

#!/bin/bash

OLDIFS=$IFS
IFS=' '
ARR=($@)
        for i in "${ARR[@]}"
        do
        echo $i >>/output/file.txt
done
IFS=$OLDIFS

This Script will output all you input line by line.

example call: thisscript.sh $(command that generates your IP's)

3
  • This is more or less equivalent to somecommand >>/output/file.txt
    – Kusalananda
    Apr 10, 2018 at 8:18
  • no, because 'some command >>/file' will write the commands output to the file in original format. My small script can handle the format input by changing the IFS env variable and outputs a line for any item.
    – Danloc
    Apr 10, 2018 at 9:51
  • Um... The output of the command is one IP address per line, so it's actually exactly equivalent to just appending the output of the command to the end of the file. Also, the task is to insert these IP addresses into a JSON document in a particular format.
    – Kusalananda
    Apr 10, 2018 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.