String 替换字符串

String 替换字符串,string,bash,sed,String,Bash,Sed,我有以下代码片段 angular.module('workflow-render', ['ng'] ).directive('workflowRender', ['$parse', '$http', '$sce', '$timeout', function ($parse, $http, $sce, $timeout) { return { restrict: 'EA', replace: false, scope: { retVal : '='

我有以下代码片段

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>

我试着提出类似问题的建议,但找不到回溯特殊字符的方法

请帮忙。谢谢

sed -E -i 's@<span.*</span>@<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>@g' filename.xml
我想这就是你要找的。
-E启用正则表达式,据我所知,您希望更换部件,请尝试以下操作:

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);
        }

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

请参见-i s/pattern/replacepattern/g

我想您至少可以简化字符串以替换为中心。*,如果只有一个中心ocurrance。Peter,你能详细说明一下吗?请展示一下你试过的。你能解释一下下面这句话的实际作用吗?org=$sed's\g为便于解释,请假设变量$org被分配给一个反斜杠\并且您希望用破折号替换它-。在本例中,sed“\-”不起作用,因为单个\被处理为转义下一个字符。为了表示文字反斜杠,您需要说sed'\\-'。句子org=$sed的替换部分也需要同样的处理,尽管模板文本不包含反斜杠。我添加了repl=$sed's\g'谢谢@tshiono。我现在明白了。除了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 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);
        }