0

I have lets say 90 HTML files and I have to put one new JS script link to head section each of them. Now, i could use some F&R engine of some code compiler

Like:

find: </head>
replace it with: link to JS script</head>

But I don't like this solution cause code is mess after that.

I was wondering - how can I use grep to insert new script link right before tag in all .html files in specific dir ?

5

grep doesn't replace or modify files. Use sed:

sed -i 's|</head>|link to JS script</head>|' *.html

How it works

-i tells sed to modify files in place.

The most important sed command is substitute. It has the form s|old|new| where old is a regular expression. Here, we replace </head> with link to JS script</head>.

*.html tells sed to operate on all html files that the shell finds in the current directory.

  • Nice :) ! Thank you for clarify this . It will help me a lot in future – Michał Żywiecki Sep 1 '15 at 17:48
0

You can use Vim in Ex mode:

for q in *.html
do
  ex -sc '%s.</head>.link to JS script&.|x' "$q"
done
  1. % select all lines

  2. s substitute

  3. x save and close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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