[使用多个文件中的第一个重命名HTML文件,如果包含正斜杠,则将其替换为连字符
我有一个带有一堆html文件的文件夹:
SMG6E30A14100000000DAAT00.html
SMB6E30A14400000000DAAT00.html
SMA6E30A14400120000DAAT00.html
等...
我想根据每个文件中的第一个h2标记重命名每个文件,如果该标记包含正斜杠,则应将斜杠替换为连字符。
因此,如果SMG6E30A14100000000DAAT00.html包含
</head><body><h2>Side Impact/Sensor (Second) Replacement</h2><a name="iR01"></a><h2><b>Removal</b></h2>
我希望脚本将文件重命名为Impact-Sensor(Second)Replacement.html
并且如果(第一个h2标签之间没有斜线)
<h2>Front Seat Belt Replacement</h2>SRS components are located in this area. <a href="./SMG6E00H46400000000DAAT00.html">Review the SRS component locations</a> and the <a href="./SMG6E00H46400000000AAAT00.html">precautions and procedures</a> in the SRS before doing repairs or service.<br><br>NOTE: Check the front seat belts for damage, and replace them if necessary. Be careful not to damage them during removal and installation.<br><br><a name="iR01"></a><h2><b>Front Seat Belt</b></h2>
相应地更名为前排座椅安全带Replacement.html
如何在Linux上执行此操作?
我有一个带有一堆html文件的文件夹:SMG6E30A14100000000DAAT00.html SMB6E30A14400000000DAAT00.html SMA6E30A14400120000DAAT00.html等...我想根据...]重命名每个文件...] >>
<<<<<
以下命令返回所需的test.html文件名。
< ./test.html tr -d '\n' | grep -oP -m 1 '(?<=<h2>).*?(?=</h2>)' | head -1 | tr '/' '-'
您可以创建一个shell脚本,该脚本在循环中使用它来扫描所有文件,获取新文件名并重命名它们。
for filename in ./input/*.html; do newname=$(< ${filename} tr -d '\n' | grep -oP -m 1 '(?<=<h2>).*?(?=</h2>)' | head -1 | tr '/' '-') mv ${filename} "./output/${newname}.html" done