在两个HTML标记bash之间插入和替换HTML

时间:2017-03-23 17:25:20

标签: html bash shell

我已成功解析HTML文档,并使用此命令从中删除了我想要的元素:

#!/bin/bash
#  ParseHtml.sh
grep -o '<h2 .*>.*</h2>' Path/to/html/report.html | sed 's/\(<h2 .*>\|<\/h2>\)//g' > parseResults.txt

以下是上述解析命令的输出:

<h2 id="test-count"><span class="number">1704</span> pass</h2>
<h2 id="fail-count"><span class="number">163</span> failures</h2>

我要做的是从解析命令中获取输出,并在两个<BODY>标记之间插入并替换它:

<j:jelly xmlns:j="jelly:core" xmlns:bsh="jelly:beanshell" xmlns:st="jelly:stapler" xmlns:d="jelly:define">

<!--This is a comment. Comments are not displayed in the browser-->

<BODY>

<!--Insert and replace any pre-existing HTML --->

</BODY>

</j:jelly>

我会用什么命令来实现这个目标?我无法通过sed尝试实现这一目标。我想坚持使用bash中的方法。任何帮助都非常感激。

2 个答案:

答案 0 :(得分:0)

多种方式中的一种(至少当我理解提问权时)

如果模板文件(让我们称之为templ.html)包含:

<j:jelly xmlns:j="jelly:core" xmlns:bsh="jelly:beanshell" xmlns:st="jelly:stapler" xmlns:d="jelly:define">

<!--This is a comment. Comments are not displayed in the browser-->

<BODY>

<!--Insert and replace any pre-existing HTML --->

</BODY>

</j:jelly>

并且您的parseResults.txt包含

<h2 id="test-count"><span class="number">1704</span> pass</h2>
<h2 id="fail-count"><span class="number">163</span> failures</h2>

然后是以下bash

#!/bin/bash
template=$(<templ.html)
results=$(<parseResults.txt)
echo "${template//<!--Insert and replace any pre-existing HTML --->/$results}"

产生

<j:jelly xmlns:j="jelly:core" xmlns:bsh="jelly:beanshell" xmlns:st="jelly:stapler" xmlns:d="jelly:define">

<!--This is a comment. Comments are not displayed in the browser-->

<BODY>

<h2 id="test-count"><span class="number">1704</span> pass</h2>
<h2 id="fail-count"><span class="number">163</span> failures</h2>

</BODY>

</j:jelly>

答案 1 :(得分:0)

 #Begin html parse. Find all h2 tags with class info and strip them out. Find all span tags (ending in R in my case) and strip them out. Finally, write the parsed HTML into a file called "iOS_Unit_Test_Results.txt" (which is essentially just a sequence of numbers)

  grep -o '<h2 .*>.*</h2>' $dirpath/something/htmltoparse.html | sed 's/\(<h2 .*>\|<\/h2>\)//g' | grep -o 'r">.*<' | grep -o 'r">.*</span>' | sed 's/r">//' | sed 's/<\/span>//' > ~/.jenkins/email-templates/iOS_Unit_Test_Results.txt

    IFS=$'\n'
    s=0

    #Create variable to write beginning header information. 

    BEGINNING_HTML="<j:jelly xmlns:j=\"jelly:core\" xmlns:bsh=\"jelly:beanshell\" xmlns:st=\"jelly:stapler\" xmlns:d=\"jelly:define\">\n<!--This is a comment. Comments are not displayed in the browser-->\n<BODY>"

ENDING_HTML="</BODY>\n</j:jelly>"

MODIFY_HTML=""

#Loop through the sequenced number file from the HTML parse above

for number in $(cat ~/.jenkins/email-templates/iOS_Unit_Test_Results.txt); do
#If it's the very first element, than it's the total number of tests.
if [ $s -eq 0 ]; then

MODIFY_HTML="$MODIFY_HTML <h2>Number of Of Tests: $number </h2>\n"
echo $MODIFY_HTML
fi
#If its the second element, than it's the # of unit test failures. 
if [ $s -eq 1 ]; then

MODIFY_HTML="$MODIFY_HTML <h2>Number of Failures: $number </h2>\n"

echo $MODIFY_HTML

fi

s=$((s+1))

done
#Write html file by concatenating variables. 

printf "$BEGINNING_HTML\n$MODIFY_HTML\n$ENDING_HTML" > ~/.jenkins/email-templates/iphoneUnitTest.jelly