Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sed不替换HTML标记_Html_Sed - Fatal编程技术网

sed不替换HTML标记

sed不替换HTML标记,html,sed,Html,Sed,嗨,我正在尝试用另一个标签替换一个标签;放置替换字符串和需要插入的字符串,如下所示: ReplaceString='\<link rel=\"stylesheet"\ type=\"text\/css"\ title=\"Style"\ href=\"..\/stylesheet\.css">' InsertStyle='\<Style>body { font:normal 68% verdana,arial,helvetica; color:#000000; } ta

嗨,我正在尝试用另一个标签替换一个标签;放置替换字符串和需要插入的字符串,如下所示:

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>'
更多/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>

单元测试结果:B2B_PIV_PROD.Run_PIV

不工作;sed似乎不理解$ReplaceString。

sed是一个错误的工具,因为它不能对字符串进行操作,并且试图表现为对字符串进行操作是一场噩梦,请参阅。此外,您也不能在您认为可能是regexp元字符的每个字符前面都加上反斜杠,因为如果出现错误,您可能会将该字符转换为元字符(例如,
我建议使用XML/HTML解析器(xmlstarlet,xmllint…)。感谢您的建议;您能为我提供一些其他建议吗?我正在使用bash更新此html文件。如果您希望
sed
查看
$ReplaceString
的内容,请不要使用单引号。请使用双引号。awk似乎更可靠;它工作正常;您能解释一下吗?它将如何将这些更改保存到t由于我可以看到该文件未被修改,因此该文件被删除。
index()
在文件中查找字符串,并返回该字符串在该行中开始位置的索引。
substr()
创建具有特定起始点和字符数的行的子字符串。然后,
$0=
赋值将行与旧字符串之前的部分、新字符串、旧字符串之后的部分重新组合在一起。使用任何UNIX命令,您都可以执行
命令文件>tmp和&mv tmp文件
来更改e原始文件。如果您有GNU awk,您可以使用
awk-i inplace…
来执行此操作。
<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>
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>