Use sed to insert analytics code into most html files
I have so many static html files (over a thousand) without Google Analytics code.
My task is not all html files without Google Analytics code. this is the folder structure of the files in which the Google Analytics code should be added:
user/[user ID]/sites/[site ID]/
these folders contain more than one html file. I can't just use sed
for all html files, because the following files already have Google Analytics code in the same users folder:
user/[user ID]/editor/index.html
Also, my html files end on one line.
How can I add js code (like Analytics) right before this and exclude all files user/[user ID]/editor/index.html
from the process?
source to share
Use find
and exclude the correct 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 this find, say 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' "{}" \;
source to share