Home > Net >  Add to file using linux command
Add to file using linux command

Time:03-30

I have the below file

{
   "param-key1": [{
   "parm-value1": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}],
    "param-key2": [{
   "parm-value2": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}],
   "param-key3": [{
   "parm-value3": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}]
}

Now i want to add(in above file) like below which is in different file.

 "param-key4": [{
   "parm-value4": [{
        "dev": "06:00",
        "prod": "09:00"
    }]

Need some help to achieve this using sed or awk or using other linux command. Unfortunately we cannot use jq in my account.

Expected output.


{
     "param-key1": [{
   "parm-value1": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}],
    "param-key2": [{
   "parm-value2": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}],
   "param-key3": [{
   "parm-value3": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}],
"param-key4": [{
   "parm-value4": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}

CodePudding user response:

This may be what you're trying to do, using any awk in any shell on every Unix box:

$ awk 'NR==FNR{new=new $0 ORS; next} /^}]$/{printf "}],\n%s", new} 1' file2 file1
{
   "param-key1": [{
   "parm-value1": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}],
    "param-key2": [{
   "parm-value2": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}],
   "param-key3": [{
   "parm-value3": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}],
 "param-key4": [{
   "parm-value4": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
}]
}

CodePudding user response:

use "ed":

ed filename << EOF
/}$
i
"param-key4": [{
   "parm-value4": [{
        "dev": "06:00",
        "prod": "09:00"
    }]
.
w
q
EOF

note: untested

  • Related