使用 SED 去除 HTML 内容
Strip HTML contents using SED
我正在执行 SED 是指定工具的任务。
任务是剥离任何网页文件(*.htm 或 *.html)的内容,并将所需数据插入新文件。
<body>
标签之前的所有内容都将被删除。
- 来自
</body>
标签的所有内容都将被删除。
下面是一个示例,其中要保留 <div>
个标签以及它们之间的内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SED Challange</title>
</head>
<body style="background-color:black;"><div style="width:100%; height:150px; margin-top:150px; text-align:center">
<img src="pic.png" width="50" height="50" alt="Pic alt text" />
</div></body></html>
但是,我无法删除 <body>
以及之前的内容:
sed 's/.*body.*>//' ./index.html > ./index.html.nobody
包含 <body>
和 </body>
的两行已被删除,而不是预期的结果!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SED Challange</title>
</head>
<img src="pic.png" width="50" height="50" alt="Pic alt text" />
我不明白为什么连一个人都会这样。感谢任何反馈。
编辑:
感谢 SLePort,这是我的完整脚本:
#!/bin/bash
#Search location as user provided argument.
target=""
#Recursive, case insensitive search for file extension like htm(l).
hit=$(find $target -type f -iname '*.htm' -or -iname '*.html')
for h in $hit
do
hp=$(realpath $h) #Absolute path of file (hit path).
echo "Stripping performed on $hp" #Informing what file(s) found.
nobody="${hp}_nobody" #File to contain desired data ending with "_nobody".
#Remove file contents from start to and including head-tag,
#Remove body-tag,
#Remove end html-tag,
#Removee blank lines,
#Insert data from file to file_nobody.
sed '1,/<\/head>/d;s/<\/*body[^>]*>//g;s/<\/html>//;/^$/d' $h > $nobody
done
此 sed 应适用于给定的代码:
sed '1,/<\/head>/d;s/<\/*body[^>]*>//g;s/<\/html>//' ./index.html > ./index.html.nobody
它删除了:
- 行从第 1 行到
</head>
标记
<body>
和 </body>
标签
</html>
结束标记
但请注意 sed 不用于解析 html 文件。请改用 xml 解析器(例如:xmllint, XMLStarlet,...)
我正在执行 SED 是指定工具的任务。 任务是剥离任何网页文件(*.htm 或 *.html)的内容,并将所需数据插入新文件。
<body>
标签之前的所有内容都将被删除。- 来自
</body>
标签的所有内容都将被删除。
下面是一个示例,其中要保留 <div>
个标签以及它们之间的内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SED Challange</title>
</head>
<body style="background-color:black;"><div style="width:100%; height:150px; margin-top:150px; text-align:center">
<img src="pic.png" width="50" height="50" alt="Pic alt text" />
</div></body></html>
但是,我无法删除 <body>
以及之前的内容:
sed 's/.*body.*>//' ./index.html > ./index.html.nobody
包含 <body>
和 </body>
的两行已被删除,而不是预期的结果!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SED Challange</title>
</head>
<img src="pic.png" width="50" height="50" alt="Pic alt text" />
我不明白为什么连一个人都会这样。感谢任何反馈。
编辑:
感谢 SLePort,这是我的完整脚本:
#!/bin/bash
#Search location as user provided argument.
target=""
#Recursive, case insensitive search for file extension like htm(l).
hit=$(find $target -type f -iname '*.htm' -or -iname '*.html')
for h in $hit
do
hp=$(realpath $h) #Absolute path of file (hit path).
echo "Stripping performed on $hp" #Informing what file(s) found.
nobody="${hp}_nobody" #File to contain desired data ending with "_nobody".
#Remove file contents from start to and including head-tag,
#Remove body-tag,
#Remove end html-tag,
#Removee blank lines,
#Insert data from file to file_nobody.
sed '1,/<\/head>/d;s/<\/*body[^>]*>//g;s/<\/html>//;/^$/d' $h > $nobody
done
此 sed 应适用于给定的代码:
sed '1,/<\/head>/d;s/<\/*body[^>]*>//g;s/<\/html>//' ./index.html > ./index.html.nobody
它删除了:
- 行从第 1 行到
</head>
标记 <body>
和</body>
标签</html>
结束标记
但请注意 sed 不用于解析 html 文件。请改用 xml 解析器(例如:xmllint, XMLStarlet,...)