简体   繁体   English

sed替换字符串

[英]sed replace a string

I have the following snippet 我有以下片段

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

and I want to replace the line 我想更换线

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

with

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>

I tried suggestions for similar questions but unable to find the way to backquote the special characters. 我尝试了类似问题的建议,但找不到找到反引号的方法。

Kindly help. 请帮助。 thanks 谢谢

Please try the following: 请尝试以下操作:

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

Result: 结果:

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

The benefit of this approach is you can write the replacee and the replacer as is in the script without modifying the escape sequence by hand. 这种方法的好处是,你可以写replacee和替代品如在脚本中,而无需手动修改转义序列。

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

I think this is what you're looking for. 我认为这就是您要寻找的。 -E enables regex and as far as I can tell you want to replace the <span></span> part -E启用正则表达式,据我所知,您要替换<span></span>部分

参见-is/pattern/replacepattern/g

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM