Perl -pi -e不能用星号替换行-也许我没有正确转义

时间:2018-09-30 15:26:54

标签: bash perl escaping

我一直在使用SO中提到的perl馅饼,但没有成功。我在做什么错了:

 * It is generally better to create a new file per style scope.
 *
 *= require_tree .
 *= require_self

我希望文件在执行perl pie之后最终看起来像这样

 * It is generally better to create a new file per style scope.
 *
 *= require iziToast
 *= require_tree .
 *= require_self

我像这样愚蠢的尝试浪费了一个小时:

  828  perl -pi -e 's/  *= require_tree ./  *= Require_tree ./' app/assets/stylesheets/application.css.scss
  832  perl -pi -e 's/require_tree \./Require_tree \./' app/assets/stylesheets/application.css.scss
  837  perl -pi -e 's/  \*\= require_tree \./  \*\= Require_tree \./' app/assets/stylesheets/application.css.scss
  839  perl -pi -e 's/= require_tree \./= Require_tree \./'       864  perl -pi -e 's/\*/\Q*/' app/assets/stylesheets/application.css.scss
  868  perl -pi -e 's/\*/\Q*/' app/assets/stylesheets/application.css.scss
  874  perl -pi -e 's/\*/*/' app/assets/stylesheets/application.css.scss
  876  perl -pi -e 's/\*/\*/' app/assets/stylesheets/application.css.scss
  878  perl -pi -e 's/\*/s/' app/assets/stylesheets/application.css.scss
  898  git checkout app/; perl -pi -e 's/\(\*\)/$1/' app/assets/stylesheets/application.css.scss; git diff
  900  git checkout app/; perl -pi -e 's/(*)/$1/' app/assets/stylesheets/application.css.scss; git diff
  901  git checkout app/; perl -pi -e 's/(\*)/$1/' app/assets/stylesheets/application.css.scss; git diff
  903  git checkout app/; perl -pi -e 's/(\\*)/$1/' app/assets/stylesheets/application.css.scss; git diff
  905  git checkout app/; perl -pi -e 's/\(\*\)/$1/' app/assets/stylesheets/application.css.scss; git diff
  907  git checkout app/; perl -pi -e 's/^(.*)/foo/' app/assets/stylesheets/application.css.scss; git diff
  908  git checkout app/; perl -pi -e 's/^  (.*)/foo/' app/assets/stylesheets/application.css.scss; git diff
  924  git checkout app/; perl -pi -e 's/^(.*)/\1/' app/assets/stylesheets/application.css.scss; git diff
  925  git checkout app/; perl -pi -e 's/^(.*)/\\1/' app/assets/stylesheets/application.css.scss; git diff

系统:Mac 10.13.5,bash 3.2,perl 5.18

注意:基于行号的方法对我来说并不理想,因为行号不是很适合未来
由于问题标题提到了perl,因此需要一个perl答案。

2 个答案:

答案 0 :(得分:3)

$sql = "SELECT `answer`, `id` FROM `answerlist` WHERE `type`= 'Help' AND `id` IN (" . implode(',', $myIds) . ") AND DATE(`added`) = DATE (NOW())"; *在正则表达式模式中是特殊的。您必须使用.\*来匹配\.*

.

perl -i -pe'print " *= require iziToast\n" if /^ \*= require_tree \.$/' file

答案 1 :(得分:0)

我什至不使用Perl;您只需使用sed

$ sed '3i\
> \ *= require iziToast
> ' tmp.txt
 * It is generally better to create a new file per style scope.
 *
 *= require iziToast
 *= require_tree .
 *= require_self