Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bunch of HTML files, and I need to add <script src="ga.js"></script> before each search instance of "</body></html>".

How would I add that to the bottom of all these files?

share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

If they're actually on the same line, you can do it pretty trivially with sed:

$ sed -i '/<\/body><\/html>/ i <script src="ga.js"></script>' *.html

That finds all html files in the current directory (*.html), searches for </body></html> (/.../), and inserts (i) the script tag before it, writing the changes back to the same file (-i)

share|improve this answer
2  
You can probably drop the search for <\/html>. Even if it turns up on another line, the important part is that this ends up before the closing body tag. –  Caleb Jul 8 '11 at 6:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.