【问题标题】:Replace Strings In One File With Strings From Other File用其他文件中的字符串替换一个文件中的字符串
【发布时间】:2019-12-07 05:20:28
【问题描述】:

我有两个文件目录,新配置文件和旧配置文件:

new-config-files/this-db/config.inc.php
new-config-files/that-db/config.inc.php
new-config-files/old-db/config.inc.php
new-config-files/new-db/config.inc.php
new-config-files/random-database/config.inc.php
etc.

old-config-files/this-db/config.inc.php
old-config-files/that-db/config.inc.php
old-config-files/old-db/config.inc.php
old-config-files/new-db/config.inc.php
old-config-files/random-database/config.inc.php
etc.

旧文件的示例 sn-p:

$cfg['Servers'][$i]['user'] = 'test-user';
$cfg['Servers'][$i]['password'] = 'test-user-password';
$cfg['Servers'][$i]['only_db'] = 'test-data';

新文件的 sn-p 示例:

$cfg['Servers'][$i]['user'] = 'DB-USER';
$cfg['Servers'][$i]['password'] = 'DB-PASSWORD';
$cfg['Servers'][$i]['only_db'] = 'DB-DATABASES';

我只想将新配置文件中的存根值(即 DB-USER、DB-PASSWORD、DB-DATABASES)替换为相应的实际值(即 test-user、test-user-password , test-data) 来自旧的配置文件。

我希望旧文件中的值都是字母数字。

当没有存根值但只有不同的值时知道如何执行此操作也很好,也应该用旧配置值替换。

【问题讨论】:

    标签: awk sed replace


    【解决方案1】:

    diffpatch 可能是完美的选择:

    diff oldfile newfile > patchfile
    

    more patchfile

    1,3c1,3
    < $cfg['Servers'][$i]['user'] = 'test-user';
    < $cfg['Servers'][$i]['password'] = 'test-user-password';
    < $cfg['Servers'][$i]['only_db'] = 'test-data';
    ---
    > $cfg['Servers'][$i]['user'] = 'DB-USER';
    > $cfg['Servers'][$i]['password'] = 'DB-PASSWORD';
    > $cfg['Servers'][$i]['only_db'] = 'DB-DATABASES';
    

    patch newfile -i patchfile -o updatedfile

    more updatedfile

    $cfg['Servers'][$i]['user'] = 'test-user';
    $cfg['Servers'][$i]['password'] = 'test-user-password';
    $cfg['Servers'][$i]['only_db'] = 'test-data';
    

    更多示例:https://linuxacademy.com/blog/linux/introduction-using-diff-and-patch/

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 2015-01-09
      • 2011-07-01
      • 2015-07-31
      • 2013-04-10
      • 1970-01-01
      • 2019-03-21
      • 2018-05-19
      • 2017-11-06
      相关资源
      最近更新 更多