我可以使用 grep 和 regex 从文件中查找某些内容并将内容写入新文件吗?
Can I use grep and regex to find certain content from an file and write the content into a new file?
我想使用正则表达式从 html 中提取一些内容,并将该内容写入新的 html。范例HTML如下:
<html>
<script src='.....'>
</script>
<style>
...
</style>
<div class='header-outer'>
<div class='header-title'>
<div class='post-content'>
<noscript>
<p>content we want</p>
</noscript>
</div>
</div></div>
<div class='footer'>
</div>
</html>
我可以使用 grep 将 <div class='post-content'>
和 </div>
之间的内容 select 写入新的 html 中吗?所以新的 html 看起来像这样:
<div class='post-content'>
<noscript>
<p>content we want</p>
</noscript>
</div>
我对 Stack overflow 做了一些研究,发现了一些可能对我的问题有帮助的代码,比如
grep -L -Z -r "<div class='post-content'>.*?<\/noscript><\/dive>" .| xargs -0 -I{} mv {} DIR
?
是否正确?如果是,xargs
部分是什么意思?谢谢,期待您的回复!
你可以使用这个 GNU sed
sed -n "/<div class='post-content'>/,/<\/div>/p" file.html > output.html
-n 没有打印
p 是打印
范围内的那些行
我想使用正则表达式从 html 中提取一些内容,并将该内容写入新的 html。范例HTML如下:
<html>
<script src='.....'>
</script>
<style>
...
</style>
<div class='header-outer'>
<div class='header-title'>
<div class='post-content'>
<noscript>
<p>content we want</p>
</noscript>
</div>
</div></div>
<div class='footer'>
</div>
</html>
我可以使用 grep 将 <div class='post-content'>
和 </div>
之间的内容 select 写入新的 html 中吗?所以新的 html 看起来像这样:
<div class='post-content'>
<noscript>
<p>content we want</p>
</noscript>
</div>
我对 Stack overflow 做了一些研究,发现了一些可能对我的问题有帮助的代码,比如
grep -L -Z -r "<div class='post-content'>.*?<\/noscript><\/dive>" .| xargs -0 -I{} mv {} DIR
?
是否正确?如果是,xargs
部分是什么意思?谢谢,期待您的回复!
你可以使用这个 GNU sed
sed -n "/<div class='post-content'>/,/<\/div>/p" file.html > output.html
-n 没有打印
p 是打印