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

时间:2017-03-08 10:54:51

标签: linux sed

我得到一个包含以下内容的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,但找不到正确的语法,你能帮助我吗?

非常感谢

4 个答案:

答案 0 :(得分:1)

尝试:

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

答案 1 :(得分:0)

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',
  );

答案 2 :(得分:0)

使用sed

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

使用-i进行就地编辑。

答案 3 :(得分:0)

async

完全符合预期,谢谢你RavinderSingh13!