0

On a solaris HTML file, how do I insert between an html tag and head tag? through bash scripting.

I have an html tag that looks like this:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:arf="http://scap.nist....-format/1.1" lang="en">

I want to insert a script written in text file, inside this 'SOLARIS' run HTML file, AFTER the long <html....> tag and BEFORE <head> tag, meaning BETWEEN the two tags, How do I do that?

I want to test using the bash commands to do it for one file at a time as well as multiple file in a folder directory. Will I have to use the folder path in my BASH code? Will I have to use FOR loop or interation in my BASH code? What would be the right code. Note: it is SOLARIS run html file not LINUX one. The name of my HTML file is report.usc20.html.

These are the lines of script that I want to use that are in my backbutton_and_scroller_script.txt file:

<div id="button"><a href="http://.....index.php">Back To The Hardening Page</a></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="http://arrow.scrolltotop.com/arrow78.js"></script>
<noscript>Not seeing a <a href="http://www.scrolltotop.com/">Scroll to Top Button</a>? Go to our FAQ page for more info.</noscript>

closed as unclear what you're asking by Rui F Ribeiro, Jesse_b, Jeff Schaller, GAD3R, user34720 Feb 28 '18 at 10:35

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please be very specific in what you need to insert and where. This group is about Unix doubts, not about HTML. Please also add the HTML file and definite word/text examples of what you intend to do. This question is extremely vague. – Rui F Ribeiro Feb 27 '18 at 20:37
  • There are some issues with posting html script on this site. I am rewriting this question right now. Unable to post html codes. – XTERMINATOR2018 Feb 27 '18 at 20:42
  • I have a javascript code that I want to insert in all html files in a folder between ..<html>.. and ..<head> tag. What Linux commands do I need? to do it – XTERMINATOR2018 Feb 27 '18 at 20:46
  • Here is the code I wanted to add between <html> and <head> tags: "<div id="button"><a href="172.26.4.130/cfg2html_outputs/Hardening/php/… To The Hardening Page</a></div><script src="ajax.googleapis.com/ajax/libs/jquery/2.1.3/… type="text/javascript" src="arrow.scrolltotop.com/arrow78.js"></… seeing a <a href="scrolltotop.com/">Scroll to Top Button</a>? Go to our FAQ page for more info.</noscript> " – XTERMINATOR2018 Feb 28 '18 at 15:25
  • And do I need a pointer to all the html files? – XTERMINATOR2018 Feb 28 '18 at 15:25
1

A simple way using sed (but with some gotchas to be aware of):

sed 's/<head/your-js-text<head/'

That will insert text before your <head> tag. The major 'gotcha' to be aware of is how to properly "escape" characters that are in your javascript that might be misinterpreted by sed, eg. the single quote, and the forward / backward slash. Also, on the off-chance that your html file is mal-formed, and includes more than one <head> tag, each will have the replacement made.

EDIT: If your javascript has many forward slashes in it, you can make use of a sed feature that allows a replacement character for the forward slash in the s command. Any of the following would also work:

sed 's^<head^your-js-text<head^'

sed 's%<head%your-js-text<head%'

sed 's#<head#your-js-text<head#'

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