Sed script to delete everything in tag except the and insert script</a></h1> </div> <div class="grid fw-wrap pb8 mb16 bb bc-black-075"> <div class="grid--cell ws-nowrap mr16 mb8" title="2016-01-12 19:07:53Z"> <span class="fc-light mr2">Asked</span> <time itemprop="dateCreated" datetime="2011-06-23T23:51:16.900" class="fromnow">Jun 23 '11 at 23:51</time> </div> <div class="grid--cell ws-nowrap mr16 mb8"> <span class="fc-light mr2">Active</span> <time class="fromnow" title="2011-06-24T17:08:10.653" datetime="2011-06-24T17:08:10.653">Jun 24 '11 at 17:08</a> </div> <div class="grid--cell ws-nowrap mb8" title="Viewed 595 times"> <span class="fc-light mr2">Viewed</span> 595 times </div> </div> <div id="mainbar" role="main" aria-label="questions and answers"> <div id="question" class="question" data-questionid="6461983" data-ownerid="801984" data-score="0"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="6461983"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> <button class="js-bookmark-btn s-btn s-btn__unset c-pointer py4"> <svg aria-hidden="true" class="svg-icon iconBookmark" width="18" height="18" viewBox="0 0 18 18"><path d="M6 1a2 2 0 00-2 2v14l5-4 5 4V3a2 2 0 00-2-2H6zm3.9 3.83h2.9l-2.35 1.7.9 2.77L9 7.59l-2.35 1.7.9-2.76-2.35-1.7h2.9L9 2.06l.9 2.77z"></path></svg> <div class="js-bookmark-count mt4" data-value=""></div> </button> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I want to remove everything inside the <code><head></code> tag except the <code><title></code> in an html file, and also insert a script into the <code><head></code> tag after this is done. I don't want to delete the <code><head></code> tag itself.</p> <p>Is this possible using Sed?</p></div> <div class="mt24 mb12"> <div class="post-taglist grid gs4 gsy fd-column"> <div class="grid ps-relative"> <a href="../../questions/tagged/html" class="post-tag js-gps-track" title="show questions tagged 'html'" rel="tag">html</a> <a href="../../questions/tagged/sed" class="post-tag js-gps-track" title="show questions tagged 'sed'" rel="tag">sed</a> <a href="../../questions/tagged/awk" class="post-tag js-gps-track" title="show questions tagged 'awk'" rel="tag">awk</a> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2011-06-24T00:55:41.750" class="relativetime">Jun 24 '11 at 00:55</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">re1man</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature owner grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Jun 23 '11 at 23:51">asked Jun 23 '11 at 23:51</time> <a href="../../users/801984/re1man" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/801984.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="re1man" /> </a> <div class="s-user-card--info"> <a href="../../users/801984/re1man" class="s-user-card--link">re1man</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">2,337</li> <li class="s-award-bling s-award-bling__gold" title="11 gold badges">11</li> <li class="s-award-bling s-award-bling__silver" title="38 silver badges">38</li> <li class="s-award-bling s-award-bling__bronze" title="54 bronze badges">54</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-6461983" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="6461983" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-7591418" class="comment js-comment " data-comment-id="7591418" data-comment-owner-id="312172" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment7591418_6461983"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Are the opening tags always on a single line for head and title and the closing tags too? Or is the whole head on a single line? For a generated file, which always looks the same, it might be possible, but if you don't know, and have to handle ``-situations, it's not to recommend.</span> – <a href="../../users/312172/user-unknown" title="35,537 reputation" class="comment-user ">user unknown</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/6461983/sed-script-to-delete-everything-in-head-tag-except-the-title-and-insert-script#comment7591418_6461983"><span title="2011-06-24T00:48:52.617 License: CC BY-SA 3.0" class="relativetime-clean">Jun 24 '11 at 00:48</span></a></span> </div> </div> </li> <li id="comment-7591432" class="comment js-comment " data-comment-id="7591432" data-comment-owner-id="801984" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment7591432_6461983"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">is there any other way to do this? with or without sed?</span> – <a href="../../users/801984/re1man" title="2,337 reputation" class="comment-user owner">re1man</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/6461983/sed-script-to-delete-everything-in-head-tag-except-the-title-and-insert-script#comment7591432_6461983"><span title="2011-06-24T00:51:04.110 License: CC BY-SA 3.0" class="relativetime-clean">Jun 24 '11 at 00:51</span></a></span> </div> </div> </li> <li id="comment-7591443" class="comment js-comment " data-comment-id="7591443" data-comment-owner-id="801984" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment7591443_6461983"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">as in modifying html files before they are rendered</span> – <a href="../../users/801984/re1man" title="2,337 reputation" class="comment-user owner">re1man</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/6461983/sed-script-to-delete-everything-in-head-tag-except-the-title-and-insert-script#comment7591443_6461983"><span title="2011-06-24T00:52:13.127 License: CC BY-SA 3.0" class="relativetime-clean">Jun 24 '11 at 00:52</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="answers-subheader grid ai-center mb8"> <div class="grid--cell fl1"> <h2 class="mb0" data-answercount="9">2 Answers<span style="display:none;" itemprop="answerCount">2</span></h2> </div> </div> </div> <a name="6462157"></a> <div id="answer-6462157" class="answer " data-answerid="6462157" data-ownerid="256196" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="6462157"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>Using regex to parse HTML is not a good choice. See <a href="../../questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags">this famous article</a> for a full discussion</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited May 23 '17 at 12:04">edited May 23 '17 at 12:04</time> <a href="../../users/-1/community" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/-1.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Community" /> </a> <div class="s-user-card--info"> <a href="../../users/-1/community" class="s-user-card--link">Community</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> </ul> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jun 24 '11 at 00:38">answered Jun 24 '11 at 00:38</time> <a href="../../users/256196/bohemian" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/256196.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Bohemian" /> </a> <div class="s-user-card--info"> <a href="../../users/256196/bohemian" class="s-user-card--link">Bohemian</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">412,405</li> <li class="s-award-bling s-award-bling__gold" title="93 gold badges">93</li> <li class="s-award-bling s-award-bling__silver" title="575 silver badges">575</li> <li class="s-award-bling s-award-bling__bronze" title="722 bronze badges">722</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-6462157" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="6462157" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-7591378" class="comment js-comment " data-comment-id="7591378" data-comment-owner-id="312172" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment7591378_6462157"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">The article isn't famous, but a foolish rant. Why aren't arguments presented? Don't they know them? Do you know them? Then name them.</span> – <a href="../../users/312172/user-unknown" title="35,537 reputation" class="comment-user ">user unknown</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/6461983/sed-script-to-delete-everything-in-head-tag-except-the-title-and-insert-script#comment7591378_6462157"><span title="2011-06-24T00:43:06.483 License: CC BY-SA 3.0" class="relativetime-clean">Jun 24 '11 at 00:43</span></a></span> </div> </div> </li> <li id="comment-7632120" class="comment js-comment " data-comment-id="7632120" data-comment-owner-id="297323" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment7632120_6462157"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@user-unknown a simple SO search reveals what you are asking about, see http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-rege</span> – <a href="../../users/297323/fredrik-pihl" title="44,604 reputation" class="comment-user ">Fredrik Pihl</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/6461983/sed-script-to-delete-everything-in-head-tag-except-the-title-and-insert-script#comment7632120_6462157"><span title="2011-06-27T09:20:52.057 License: CC BY-SA 3.0" class="relativetime-clean">Jun 27 '11 at 09:20</span></a></span> </div> </div> </li> <li id="comment-7643154" class="comment js-comment " data-comment-id="7643154" data-comment-owner-id="312172" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment7643154_6462157"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Well, @Frederik, *that* is a useful comment, but Bohemian's isn't. It is an unprofessional waste of time.</span> – <a href="../../users/312172/user-unknown" title="35,537 reputation" class="comment-user ">user unknown</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/6461983/sed-script-to-delete-everything-in-head-tag-except-the-title-and-insert-script#comment7643154_6462157"><span title="2011-06-27T20:09:42.513 License: CC BY-SA 3.0" class="relativetime-clean">Jun 27 '11 at 20:09</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="6471279"></a> <div id="answer-6471279" class="answer " data-answerid="6471279" data-ownerid="548225" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="6471279"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I will suggest you to use a DOM Parser for this type of work since any regex you try will break at some point using sed or any of its variant. Since you've asked for an alternative in your comments consider following code in PHP:</p> <pre><code>$content = ' <HTML> <HEAD> <link href="/style.css" rel="stylesheet" type="text/css"> <title> Page Title Goes here </title> <script> var str = "ZZZZZ1233@qq.edu"; </script> </HEAD> '; $dom = new DOMDocument(); $dom->loadHTML($content); $head=' <head> <script> // your javascript goes here var x="foo"; </script> '; $headTag = $dom->getElementsByTagName("head")->item(0); if ($headTag != null) { $title = $headTag->getElementsByTagName("title")->item(0); if ($title != null) $head .= '<title>' . $title->textContent . '</title> '; } $head .= '</head>'; var_dump($head); </code></pre> <h3>OUTPUT</h3> <pre><code>string(118) " <head> <script> // your javascript goes here var x="foo"; </script> <title>Page Title Goes here</title> </head>" </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jun 24 '11 at 17:08">answered Jun 24 '11 at 17:08</time> <a href="../../users/548225/anubhava" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/548225.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="anubhava" /> </a> <div class="s-user-card--info"> <a href="../../users/548225/anubhava" class="s-user-card--link">anubhava</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">761,203</li> <li class="s-award-bling s-award-bling__gold" title="64 gold badges">64</li> <li class="s-award-bling s-award-bling__silver" title="569 silver badges">569</li> <li class="s-award-bling s-award-bling__bronze" title="643 bronze badges">643</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>