首页 > 解决方案 > sed 替换一个字符串

问题描述

我有以下片段

angular.module('workflow-render', ['ng'] ).directive('workflowRender', ['$parse', '$http', '$sce', '$timeout', function ($parse, $http, $sce, $timeout) {
  return {
    restrict: 'EA',
    replace: false,
    scope: {
      retVal : '='
    },
    template: '<div data-ng-if="displayError" style="min-height:150px;"><center><span data-i18n-content="{bundle : \'widgets\', key : \'workflow/Error\'}"></span></center></div>',
    link: function($scope, elem, attrs) {
      $scope.$watch( 'retVal' , function( newVal ) {
        if ( newVal ) {
          Render($scope.retVal);
        }

我想换行

template: '<div data-ng-if="displayError" style="min-height:150px;"><center><span data-i18n-content="{bundle : \'widgets\', key : \'workflow/Error\'}"></span></center></div>

template: '<div data-ng-if="displayError" style="min-height:150px;"><center><span style="color:darkolivegreen;font-weight:bold">Sorry, unable to render workflow due to presence of cyclic workflow transition. We will fix this soon.</span></center></div>

我尝试了类似问题的建议,但找不到反引号特殊字符的方法。

请帮忙。谢谢

标签: stringbashsed

解决方案


请尝试以下方法:

org=$(cat << 'EOS'
template: '<div data-ng-if="displayError" style="min-height:150px;"><center><span data-i18n-content="{bundle : \'widgets\', key : \'workflow/Error\'}"></span></center></div>'
EOS
)

repl=$(cat << 'EOS'
template: '<div data-ng-if="displayError" style="min-height:150px;"><center><span style="color:darkolivegreen;font-weight:bold">Sorry, unable to render workflow due to presence of cyclic workflow transition. We will fix this soon.</span></center></div>'
EOS
)

org=$(sed 's#\\#\\\\#g' <<< "$org")
repl=$(sed 's#\\#\\\\#g' <<< "$repl")
sed "s#$org#$repl#" sample.txt

结果:

angular.module('workflow-render', ['ng'] ).directive('workflowRender', ['$parse', '$http', '$sce', '$timeout', function ($parse, $http, $sce, $timeout) {
  return {
    restrict: 'EA',
    replace: false,
    scope: {
      retVal : '='
    },
    template: '<div data-ng-if="displayError" style="min-height:150px;"><center><span style="color:darkolivegreen;font-weight:bold">Sorry, unable to render workflow due to presence of cyclic workflow transition. We will fix this soon.</span></center></div>',
    link: function($scope, elem, attrs) {
      $scope.$watch( 'retVal' , function( newVal ) {
        if ( newVal ) {
          Render($scope.retVal);
        }

这种方法的好处是您可以在脚本中按原样编写replacee 和replacer 而无需手动修改转义序列。


推荐阅读