I have so many static html files (well over a thousand) without Google Analytics code.
我有很多静态html文件(超过一千)没有Google Analytics代码。
my challenge is, not all html files are without Analytics code. this is the folder structure of the files that should have Analytics code added:
我的挑战是,并非所有的html文件都没有Google Analytics代码。这是应添加Google Analytics代码的文件的文件夹结构:
user/[user ID]/sites/[site ID]/
用户/ [用户ID] / sites / [网站ID] /
there are more than one html files in those folders. I can't simply use sed
on all html files because following files in same "users" folder already have the Analytics js code:
这些文件夹中有多个html文件。我不能简单地在所有html文件上使用sed,因为同一“users”文件夹中的后续文件已经有了Analytics js代码:
user/[user ID]/editor/index.html
moreover, my html files end with in one line.
而且,我的html文件以一行结尾。
How can I add a js code (i.e. Analytics) right before and exclude all theuser/[user ID]/editor/index.html
files from the process?
如何在此之前添加js代码(即Analytics)并从流程中排除所有用户/ [用户ID] /editor/index.html文件?
1 个解决方案
#1
Use find
and exclude the proper files.
使用查找并排除正确的文件。
find . -type f -regextype sed -regex '.*users/[0-9]*/sites/[0-9]*/.*html' -exec sed -i 's/pattern/replace/g' "{}" \;
You can always exclude files from that find, say files .*/sites/.*/index.html
:
您始终可以从该查找中排除文件,例如文件。* / 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' "{}" \;
#1
Use find
and exclude the proper files.
使用查找并排除正确的文件。
find . -type f -regextype sed -regex '.*users/[0-9]*/sites/[0-9]*/.*html' -exec sed -i 's/pattern/replace/g' "{}" \;
You can always exclude files from that find, say files .*/sites/.*/index.html
:
您始终可以从该查找中排除文件,例如文件。* / 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' "{}" \;