Saturday, February 24, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 2689  / 12 Months ago, mon, march 27, 2023, 9:42:14

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 ?


More From » command-line

 Answers
2

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.


[#18526] Wednesday, March 29, 2023, 12 Months  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
defendle

Total Points: 219
Total Questions: 131
Total Answers: 112

Location: Finland
Member since Sat, Nov 6, 2021
2 Years ago
;