[英]Add a line of code after the first <?php line
我需要更新一个要移到新主机的网站。 我想在第一次出现<?php
之后,将以下行添加到每个文件,仅一次:
define('FRAMEWORK_LOCATION', '/home/someUser/framework.php');
我已经尝试过这种Perl oneliner的几种变体:
$ find . -name '*' -print0 | xargs -0 perl -pi -e 's|<?php|<?php\rdefine('\''FRAMEWORK_LOCATION'\'', '/home/someUser/framework.php');'
但是,可以看出它将影响所有行。 我不特别担心在初始<?php
之后是否需要附加代码,但是如果解决方案确实考虑到了这一点,那么对我来说也很有益。
perl -ie 'undef $/; $txt = <>; $txt =~ s|<?php|<?php\rdefine("FRAMEWORK_LOCATION", "/home/someUser/framework.php")|; print $txt;'
要么
perl -ie '$first = 1; while (<>) { if ($first && s|<?php|<?php\rdefine("FRAMEWORK_LOCATION", "/home/someUser/framework.php")|) { $first= 0; } print; }'
试试这个:
find . -name "*" -print0 | xargs -0 sed '0,/<?php/s/<?php/<?php\n\tdefine(...)/'
您可以直接使用sed,而无需调用perl:
$ find . -name '*' -type f -print0 | xargs -0 sed -e '0,/<?php/s||&\ndefine("FRAMEWORK_LOCATION","/home/someUser/framework.php")|'
相关的位是:
sed -e '0,/<?php/s||&\nNEW_TEXT|'
您指定的位置:
0,/<?php
:从第一行到<?php
的第一次出现 s||&\\nNEW_TEXT|
:您将之前的匹配<?php
替换为自身,并在其后一行添加一些新文本。 请注意,我添加了switch -type f
来find
过滤目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.