Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-24-2014, 02:19 PM   #1
LQ Newbie
Registered: Sep 2014
Posts: 1

Rep: Reputation: Disabled
Use SED to insert textfile before a line, using a COMBINED search

I have a text file, which contains HTML markup, including multiple DIVs.
Just before the _last_ end-DIV tag before a particular line, I want to insert the contents of a text file.
Example: original.html contains this:
...(lots of HTML)
</div> {include file="inc/Footer.txt"}
{include file="inc/OtherFooter.txt"}
...(more HTML)

I have another file, newstuff.html, which contains:
New stuff

I want to find the _last_ "</div>" tag before the "inc/Footer.txt" line, and insert the contents of "newstuff.html" before that </div>. Result:
...(lots of HTML)
New stuff
</div> {include file="inc/Footer.txt"}
{include file="inc/OtherFooter.txt"}
...(more HTML)

NOTE: there is an additional trick here, in that the "</div>" may be on the SAME line as the "inc/Footer.txt", or the line before. There will be nothing but whitespace between the </div> and the '{include file="inc/Footer.txt"}' line.

Using "sed '/Footer.txt/i newtextstuff' original.html" would insert "newtextstuff" before the line containing "/Footer.txt", but (a) I want to insert the contents of a file, and (b) I can't rely on the </div> being on the same line as the '/Footer.txt'.
Old 09-26-2014, 05:18 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
Some good advice here
Basically, use a proper tool for the job; trying just eg sed is way too fragile.
Old 09-27-2014, 07:17 AM   #3
LQ Addict
Registered: Dec 2013
Posts: 5,936

Rep: Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409
me: good experiences with html-xml-utils.

maybe this could also be solved with server-side scripting, e.g. php?

not sure what you're trying to achieve.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script - Cant get sed to replace a line from a textfile CrowdstarQA Programming 4 02-23-2012 11:53 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
sed to search and insert column Mecalith Programming 15 02-24-2011 10:52 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration