如何在 Linux 上的多个文件中搜索和替换长 HTML
How to search and replace long HTML in multiple files on Linux
我需要用这个 HTML:
递归查找所有文件
<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
并将其替换为 HTML:
<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="date=no">
<meta name="format-detection" content="address=no">
<meta name="format-detection" content="email=no">
这是我尝试将 grep 命令通过管道传输到 sed 的失败尝试:
grep --include="index.html" -PRwzl -e '<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>\n <meta charset="utf-8">\n <meta name="google" value="notranslate">\n' | xargs -i@ sed -i 's/<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>\n <meta charset="utf-8">\n <meta name="google" value="notranslate">\n/<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>\n <meta charset="utf-8">\n <meta name="google" value="notranslate">\n <meta name="google" value="notranslate">\n <meta name="format-detection" content="telephone=no">\n <meta name="format-detection" content="date=no">\n <meta name="format-detection" content="address=no">\n <meta name="format-detection" content="email=no">\n/g' @
单独使用 grep 命令效果很好。
为清楚起见,这里是分成许多部分的命令。:
grep --include="index.html" \
-PRwzl \
-e '<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
\n <meta charset="utf-8">
\n <meta name="google" value="notranslate">
\n' \
| xargs -i@ sed -i 's/<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
\n <meta charset="utf-8">
\n <meta name="google" value="notranslate">
\n
/<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
\n <meta charset="utf-8">
\n <meta name="google" value="notranslate">
\n <meta name="google" value="notranslate">
\n <meta name="format-detection" content="telephone=no">
\n <meta name="format-detection" content="date=no">
\n <meta name="format-detection" content="address=no">
\n <meta name="format-detection" content="email=no">
\n
/g' @
你的命令白白的很复杂。您可以在文件中 运行 您的 sed
,而不需要前面的 grep
和 xargs
。通常,使用 sed
对文件进行内联编辑类似于:
sed -i 's/TO_FIND/REPLACE/' FILE.txt
另一条评论,sed
不是编辑 HTML 的好工具。看看RegEx match open tags except XHTML self-contained tags.
话虽这么说,我建议这个脚本来满足您的要求。
#!/bin/bash
#
find . -type f -name "*.html" -print0 | while IFS= read -r -d '' file
do
if [[ $(grep -c 'id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"' $file) -ne 0 ]]
then
# Add the content...
echo "Adding in file $file"
sed -i 's#</head># <meta name="format-detection" content="telephone=no">\n <meta name="format-detection" content="date=no">\n <meta name="format-detection" content="address=no">\n <meta name="format-detection" content="email=no">\n </head>#' "$file"
else
echo "Nothing to do on $file"
fi
done
- 将
find
与 while
和 read
一起使用涵盖了子目录中有 HTML 个文件的情况。
grep
已经过高度简化。如果存在 id 和 class 值,则足以识别有效文件。
- 然后在
sed
中,您可以只添加新行。您的 sed
用这些相同的行替换了行。
- 我在
sed
中使用 #
作为分隔符而不是 /
以避免与 HTML 代码混淆。
- 这是基于我自己创建的文件,因为您没有提供示例。您应该在问题中提供示例。
<head>
部分中的标签顺序无关紧要,因此在结束 </head>
之前添加行即可。
- 显然
else
部分是可选的。
<opinion>
我发现这种类型的脚本比长单行更容易理解和调试。</opinion>
。
假设index.html是:
<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
<title>TITRE</title>
</head>
<body>
<p>PARAGRAPH</p>
</body>
</html>
结果是:
<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
<title>TITRE</title>
<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="date=no">
<meta name="format-detection" content="address=no">
<meta name="format-detection" content="email=no">
</head>
<body>
<p>PARAGRAPH</p>
</body>
</html>
我需要用这个 HTML:
递归查找所有文件<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
并将其替换为 HTML:
<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="date=no">
<meta name="format-detection" content="address=no">
<meta name="format-detection" content="email=no">
这是我尝试将 grep 命令通过管道传输到 sed 的失败尝试:
grep --include="index.html" -PRwzl -e '<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>\n <meta charset="utf-8">\n <meta name="google" value="notranslate">\n' | xargs -i@ sed -i 's/<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>\n <meta charset="utf-8">\n <meta name="google" value="notranslate">\n/<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>\n <meta charset="utf-8">\n <meta name="google" value="notranslate">\n <meta name="google" value="notranslate">\n <meta name="format-detection" content="telephone=no">\n <meta name="format-detection" content="date=no">\n <meta name="format-detection" content="address=no">\n <meta name="format-detection" content="email=no">\n/g' @
单独使用 grep 命令效果很好。
为清楚起见,这里是分成许多部分的命令。:
grep --include="index.html" \
-PRwzl \
-e '<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
\n <meta charset="utf-8">
\n <meta name="google" value="notranslate">
\n' \
| xargs -i@ sed -i 's/<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
\n <meta charset="utf-8">
\n <meta name="google" value="notranslate">
\n
/<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
\n <meta charset="utf-8">
\n <meta name="google" value="notranslate">
\n <meta name="google" value="notranslate">
\n <meta name="format-detection" content="telephone=no">
\n <meta name="format-detection" content="date=no">
\n <meta name="format-detection" content="address=no">
\n <meta name="format-detection" content="email=no">
\n
/g' @
你的命令白白的很复杂。您可以在文件中 运行 您的 sed
,而不需要前面的 grep
和 xargs
。通常,使用 sed
对文件进行内联编辑类似于:
sed -i 's/TO_FIND/REPLACE/' FILE.txt
另一条评论,sed
不是编辑 HTML 的好工具。看看RegEx match open tags except XHTML self-contained tags.
话虽这么说,我建议这个脚本来满足您的要求。
#!/bin/bash
#
find . -type f -name "*.html" -print0 | while IFS= read -r -d '' file
do
if [[ $(grep -c 'id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"' $file) -ne 0 ]]
then
# Add the content...
echo "Adding in file $file"
sed -i 's#</head># <meta name="format-detection" content="telephone=no">\n <meta name="format-detection" content="date=no">\n <meta name="format-detection" content="address=no">\n <meta name="format-detection" content="email=no">\n </head>#' "$file"
else
echo "Nothing to do on $file"
fi
done
- 将
find
与while
和read
一起使用涵盖了子目录中有 HTML 个文件的情况。 grep
已经过高度简化。如果存在 id 和 class 值,则足以识别有效文件。- 然后在
sed
中,您可以只添加新行。您的sed
用这些相同的行替换了行。 - 我在
sed
中使用#
作为分隔符而不是/
以避免与 HTML 代码混淆。 - 这是基于我自己创建的文件,因为您没有提供示例。您应该在问题中提供示例。
<head>
部分中的标签顺序无关紧要,因此在结束</head>
之前添加行即可。- 显然
else
部分是可选的。 <opinion>
我发现这种类型的脚本比长单行更容易理解和调试。</opinion>
。
假设index.html是:
<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
<title>TITRE</title>
</head>
<body>
<p>PARAGRAPH</p>
</body>
</html>
结果是:
<html id="blx-5fb3c619e82a2863d6567c52-000000001" class="blx-5fb3c619e82a2863d6567c52"><head>
<meta charset="utf-8">
<meta name="google" value="notranslate">
<title>TITRE</title>
<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="date=no">
<meta name="format-detection" content="address=no">
<meta name="format-detection" content="email=no">
</head>
<body>
<p>PARAGRAPH</p>
</body>
</html>