'Add to file using linux command
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"
}]
}
Solution 1:[1]
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"
}]
}]
}
Solution 2:[2]
use "ed":
ed filename << EOF
/}$
i
"param-key4": [{
"parm-value4": [{
"dev": "06:00",
"prod": "09:00"
}]
.
w
q
EOF
note: untested
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ed Morton |
| Solution 2 | Sigi |
