0

I would like to ask if it's possible to delete all strings between two strings even on different line ?

Here is the original file:

<?php
$CONFIG = array (
  'trusted_domains' =>
  array (
    0 => '192.168.0.32',
  ),
  'datadirectory' => '/var/www/html/files/data',
  'overwrite.cli.url' => 'http://192.168.0.44/files',
  'dbtype' => 'mysql',
  'dbport' => '',
  'installed' => true,
  'loglevel' => 2,
  'maintenance' => false,
);

What I expect :

<?php
$CONFIG = array (
  'trusted_domains' =>
  array (

  ),
  'datadirectory' => '/var/www/html/files/data',
  'overwrite.cli.url' => 'http://192.168.0.44/files',
  'dbtype' => 'mysql',
  'dbport' => '',
  'installed' => true,
  'loglevel' => 2,
  'maintenance' => false,
);

I want to empty everything inside trusted domains.

What I tried :

sed -e 's/\(trusted_domains\).*\(\),\)/\1\2/'
0

2 Answers 2

1

By default, sed operates only line by line. There are some commands and options using which you can operate on multiple lines. For example, if your sed supports -z option and input doesn't contain the ASCII NUL character, you can do this:

$ sed -zE 's/(trusted_domains[^(]+\()[^)]+/\1/' ip.txt
<?php
$CONFIG = array (
  'trusted_domains' =>
  array (),
  'datadirectory' => '/var/www/html/files/data',
  'overwrite.cli.url' => 'http://192.168.0.44/files',
  'dbtype' => 'mysql',
  'dbport' => '',
  'installed' => true,
  'loglevel' => 2,
  'maintenance' => false,
);

This will slurp entire input content and sed would see only one input record. So, this will not be suitable for large input files. Also, here it is assumed that parenthesis are not part of contents of array


Perl has an option to slurp entire input content irrespective of characters present:

perl -0777 -pe 's/trusted_domains[^(]+\(\K[^)]+//' ip.txt
Sign up to request clarification or add additional context in comments.

Comments

1

It might be easier using perl:

perl -0777 -pe 's/(trusted_domains.*=>\s*array\s*\()[\s\S]*\),/$1\n  ),/' file.php

<?php
$CONFIG = array (
  'trusted_domains' =>
  array (
  ),
  'datadirectory' => '/var/www/html/files/data',
  'overwrite.cli.url' => 'http://192.168.0.44/files',
  'dbtype' => 'mysql',
  'dbport' => '',
  'installed' => true,
  'loglevel' => 2,
  'maintenance' => false,
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.