嗨,我正在尝试用另一个标签替换标签;放置替换字符串和需要插入的字符串,如下所示:
ReplaceString='\<link rel=\"stylesheet"\ type=\"text\/css"\ title=\"Style"\ href=\"..\/stylesheet\.css">'
InsertStyle='\<Style>body { font:normal 68% verdana,arial,helvetica; color:#000000; } table tr td, table tr th { font-size: 68%; } table\.details tr th{ font-weight: bold; text-align:left; background:#a6caf0; } table\.details tr td{ background:#eeeee0; } p { line-height:1\.5em; margin-top:0\.5em; margin-bottom:1\.0em; } h1 { margin: 0px 0px 5px; font: 165% verdana,arial,helvetica } h2 { margin-top: 1em; margin-bottom: 0\.5em; font: bold 125% verdana,arial,helvetica } h3 { margin-bottom: 0\.5em; font: bold 115% verdana,arial,helvetica } h4 { margin-bottom: 0\.5em; font: bold 100% verdana,arial,helvetica } h5 { margin-bottom: 0\.5em; font: bold 100% verdana,arial,helvetica } h6 { margin-bottom: 0\.5em; font: bold 100% verdana,arial,helvetica } \.Error { font-weight:bold; color:red; } \.Failure { font-weight:bold; color:purple; } \.Properties { text-align:right; }<\/Style>'
使用命令:
sed -i 's/$ReplaceString/$InsertStyle/g' /tmp/test.html
更多/tmp/test.html
<html xmlns:string="xalan://java.lang.String" xmlns:lxslt="http://xml.apache.org/xslt">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Unit Test Results: B2B_PIV_PROD.Run_PIV</title>
<link rel="stylesheet" type="text/css" title="Style" href="../stylesheet.css">
</html>
不起作用;似乎sed无法理解$ ReplaceString。
答案 0 :(得分:1)
sed是错误的工具,因为它无法对字符串进行运算,并且是试图表现出好像对字符串进行操作一样的噩梦,请参见Is it possible to escape regex metacharacters reliably with sed。同样,您不能只在您认为可能是正则表达式元字符的每个字符前面加上反斜杠,因为如果输入错误,则可以将该字符转换为元字符(例如,<
是字面字符,但是当您写了\<
,就把它变成了单词边界元字符。
如果要用另一个字符串替换一个字符串,只需使用可以对字符串进行操作的工具,例如啊!
假设变量中的所有反斜杠是因为您试图让sed表现得好像它理解字符串一样,请删除它们,然后简单地:
ReplaceString='<link rel="stylesheet" type="text/css" title="Style" href="../stylesheet.css">'
InsertStyle='<Style>body { font:normal 68% verdana,arial,helvetica; color:#000000; } table tr td, table tr th { font-size: 68%; } table.details tr th{ font-weight: bold; text-align:left; background:#a6caf0; } table.details tr td{ background:#eeeee0; } p { line-height:1.5em; margin-top:0.5em; margin-bottom:1.0em; } h1 { margin: 0px 0px 5px; font: 165% verdana,arial,helvetica } h2 { margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica } h3 { margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica } h4 { margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica } h5 { margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica } h6 { margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica } .Error { font-weight:bold; color:red; } .Failure { font-weight:bold; color:purple; } .Properties { text-align:right; }</Style>'
awk -v old="$ReplaceString" -v new="$InsertStyle" '
beg = index($0,old) {
$0 = substr($0,1,beg-1) new substr($0,beg+length(old))
}
{ print }
' file
<html xmlns:string="xalan://java.lang.String" xmlns:lxslt="http://xml.apache.org/xslt">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Unit Test Results: B2B_PIV_PROD.Run_PIV</title>
<Style>body { font:normal 68% verdana,arial,helvetica; color:#000000; } table tr td, table tr th { font-size: 68%; } table.details tr th{ font-weight: bold; text-align:left; background:#a6caf0; } table.details tr td{ background:#eeeee0; } p { line-height:1.5em; margin-top:0.5em; margin-bottom:1.0em; } h1 { margin: 0px 0px 5px; font: 165% verdana,arial,helvetica } h2 { margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica } h3 { margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica } h4 { margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica } h5 { margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica } h6 { margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica } .Error { font-weight:bold; color:red; } .Failure { font-weight:bold; color:purple; } .Properties { text-align:right; }</Style>
</html>