Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use sed to insert analytics code to most html files

I have so many static html files (well over a thousand) without Google Analytics code.

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:

user/[user ID]/sites/[site 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:

user/[user ID]/editor/index.html

moreover, my html files end with in one line.

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?

like image 317
Hadi Farnoud Avatar asked Oct 14 '25 03:10

Hadi Farnoud


1 Answers

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:

find . -type f -regextype sed -regex '.*users/[0-9]*/sites/[0-9]*/.*html' -not -name "*index.html" -exec sed -i 's/pattern/replace/g' "{}" \;
like image 182
ShellFish Avatar answered Oct 16 '25 23:10

ShellFish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!