string - 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>
我尝试了类似问题的建议,但找不到反引号特殊字符的方法。
请帮忙。谢谢
解决方案
请尝试以下方法:
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 ,而无需手动修改转义序列。
推荐阅读
- javascript - 每当页面刷新时,从右侧弹出的抽屉从几分之一秒内弹出
- javascript - 根据值删除数组内的对象
- r - R:硬编码函数内变量的内容
- python - 如何创建不影响所有人的 Telegram Bot 内联键盘菜单?
- python-3.x - 为什么我的代码会返回一百万次答案?
- web - 没有基于网络的信标广播,对吧?
- python - 如何使用python从tableau仪表板下载特定工作表?
- c++ - 数组中具有十进制值的矩阵的转置
- python - 使用带有 pyinstaller 标志 --windowed 的 shutil.which("ffmpeg") 的功能(据说)
- java - handlerinterceptor prehandle 调用了两次