繁体   English   中英

用sed / awk在php文件中的两行之间添加两行

[英]add line between two 2 lines in php file with sed/awk

我得到一个包含以下内容的php文件:

<?php
  ),
  'datadirectory' => '/var/www/owncloud/data',
  'overwrite.cli.url' => 'http://M.Y.I.P',
  'dbtype' => 'mysql',
  'version' => '9.1.4.2',
  );

我想在版本和末尾之间插入一行:

  'theme' => 'mytheme',

所以它必须看起来像这样:

<?php
  ),
  'datadirectory' => '/var/www/owncloud/data',
  'overwrite.cli.url' => 'http://M.Y.I.P',
  'dbtype' => 'mysql',
  'version' => '9.1.4.2',
  'theme' => 'mytheme',
  );

我目前正在尝试使用sed或awk,但是找不到正确的语法,您能帮我吗?

非常感谢

尝试:

awk -vs1="'" '/version/{print $0 ORS "  " s1 "theme" s1"  => " s1 "mytheme" s1",";next} 1'  Input_file 

只需将字符串版本检查到Input_file中,然后将您要插入的行与当前行一起打印,如果在任何行中均未找到此字符串,则只需打印Input_file行。

编辑:如下编辑Input_file本身。

awk -vs1="'" '/version/{print $0 ORS "  " s1 "theme" s1"  => " s1 "mytheme" s1",";next} 1' Input_file > file_tmp && mv file_tmp Input_file
awk -v line="'theme'  => 'mytheme'," '1;/version/{$0="  "line;print}' inputfile
<?php
  ),
  'datadirectory' => '/var/www/owncloud/data',
  'overwrite.cli.url' => 'http://M.Y.I.P',
  'dbtype' => 'mysql',
  'version' => '9.1.4.2',
  'theme'  => 'mytheme',
  );

sed

sed -r "/^[[:space:]]*'version'/a  'theme' => 'mytheme'," file

使用-i进行就地编辑。

awk -v s1="'" '/version/{print $0 ORS "  " s1 "theme" s1"  => " s1 "mytheme" s1",";next} 1'  config.php > file_tmp && mv config.php config.php.old && mv file_tmp config.php 

刚好按预期工作,谢谢RavinderSingh13!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM