Had*_*oud 4 html bash google-analytics sed
我有很多静态 html 文件(超过一千个),没有 Google Analytics 代码。
我的挑战是,并非所有 html 文件都没有 Analytics 代码。这是应添加 Analytics 代码的文件的文件夹结构:
user/[user ID]/sites/[site ID]/
这些文件夹中有多个 html 文件。我不能简单地sed在所有 html 文件上使用,因为同一“users”文件夹中的以下文件已经具有 Analytics js 代码:
user/[user ID]/editor/index.html
此外,我的 html 文件以一行结尾。
如何在之前添加 js 代码(即 Analytics)并user/[user ID]/editor/index.html从进程中排除所有文件?
使用find并排除正确的文件。
find . -type f -regextype sed -regex '.*users/[0-9]*/sites/[0-9]*/.*html' -exec sed -i 's/pattern/replace/g' "{}" \;
Run Code Online (Sandbox Code Playgroud)
您始终可以从该查找中排除文件,例如 files .*/sites/.*/index.html:
find . -type f -regextype sed -regex '.*users/[0-9]*/sites/[0-9]*/.*html' -not -name "*index.html" -exec sed -i 's/pattern/replace/g' "{}" \;
Run Code Online (Sandbox Code Playgroud)