Issue
I would like to replace a text INSERT
in template.js
with file content from insert.json
and write it to a result.js
.
insert.json:
{
"insert": true,
"other": "you can ignore line indentation"
}
template.js:
function test() {
const x = INSERT;
console.log(x);
}
result.js:
function test() {
const x = {
"insert": true,
"other": "you can ignore line indentation"
};
console.log(x);
}
I am searching to replace INSERT
. But a alternative for example regular expression to find, is also great:
const x = (INSERT);
to replace$INSERT
(const x = )INSERT(;)
to replace$1 $INSERT $2
You can't use something like $(cat insert.json)
, because the insert.json file is huge and exeed terminal character limit.
Edit: I didn't express myself correctly. The file isn't really that big. So there is no memory problem in RAM or on the hard drive.
The file is around 200 kb. That's tiny, see:
du -h insert.json
: 196 kbwc -l insertjson
: 6416 lineswc -m insert.json
: 198740 characters
If I do this with sed...
LANG=C sed "s/INSERT/const x = $(cat insert.json)/g" template.js > result.js
...there are 2 problems:
- sed: The argument list is too long (Too much data in a terminal command)
- sed: -e expression #1, char 20: unterminated `s' command (bad character, because insert.json has special character)
Solution
It's possible to solve it straightforwardly with "$(cat insert.json)"
, without triggering the command-line arguments too long error, because the limit applies only to external commands run by the shell. Shel built-in commands are only limited by available memory.
This works in Bash, Zsh, Ksh, BusyBox sh, but not in Dash (because Dash doesn't support the //
substitution), all without any external commands other than cat
:
INSERT="$(cat insert.json)"
TEMPLATE="$(cat template.js)"
printf '%s\n' "${TEMPLATE//INSERT/$INSERT}" >result.js
In Bash, Zsh and Ksh (but not in BusyBox sh) both instances of cat
above can be replaced with <
, and then it won't use any external commands at all.
Variable assignment (=
) and the command printf
are built in to the shells above, thus they work with strings longer than the command-line argument limit.
Answered By - pts Answer Checked By - Marie Seifert (WPSolving Admin)