I am modifying a yaml file which contains this:
name: config.php
data:
config.php: <CONFIG_PHP_SECTION>
folder: test
Now, the config.php is something like this:
<?php
/* This is a config
*/
$app['env'] = 'sandbox';
$app['db'] = [
'host'=>'localhost'
]
Is there a way that I can either use awk or sed or perl to get to this:
name: config.php
data:
config.php: "<?php\n /* This is a config\n */\n $app['env'] = 'sandbox';\n $app['db'] = [\n 'host'=>'localhost'\n ]"
folder: test
I've tried using CONFIG=$(awk '{printf "%s\\n", $0}' config | tr '"' "\'")
to change newline to \n, and then sed -i "" "s&<CONFIG_PHP_SECTION>&\"$CONFIG\"&" config.yaml
... However, all I got is a weird <?phpn /* This is a confign */n $app['env'] = 'sandbox';n $app['db'] = [n 'host'=>'localhost'n ]
, a string without backward slash in front of the n.
I've also tried using tr '\n' "\\n"
but all I get is an actual newline (which breaks the yaml file)
Is there anyway I can actually get \n displayed and save to the config.yaml in bash?
Thanks
p.s. The suggested duplication ""sed" Insert Backslash to File" doesn't work for me... Could you elaborate more, @RobinGreen? Thanks!
When I did sed -i "" 's&<CONFIG_PHP_SECTION>&"$CONFIG"&' config.yaml
, the output is
name: config.php
data:
config.php: "$CONFIG"
folder: test
When I did sed -i "" 's&<CONFIG_PHP_SECTION>&"'"$CONFIG"'"&' config.yaml
, then the output is the same as the one missing \
.
Also... CONFIG is already \\n
, it's just the last sed command that both \
got deleted leaving n
behind.