0

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.

snowmen10
  • 29
  • 2
  • 5
  • @RobinGreen Your suggestion doesn't work for me, could you elaborate more? Thanks (explained in the original post) – snowmen10 Jun 03 '18 at 13:45
  • @Cyrus I would vote up/down or accept it when someone has post an answer. However, no one has answered anything when you post this... Is this like a reminder? – snowmen10 Jun 03 '18 at 13:55
  • 1
    @snowmen10: yes, it's a reminder. You asked four questions and accepted no answer. – Cyrus Jun 03 '18 at 15:37

0 Answers0