LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-24-2014, 03:19 PM   #1
wilsoncpu
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)
</table>
</div>
</div> {include file="inc/Footer.txt"}
{include file="inc/OtherFooter.txt"}
...(more HTML)

I have another file, newstuff.html, which contains:
<div>
New stuff
</div>

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)
</table>
</div>
<div>
New stuff
</div>
</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, 06:18 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Some good advice here https://unix.stackexchange.com/quest...-html-on-linux.
Basically, use a proper tool for the job; trying just eg sed is way too fragile.
 
Old 09-27-2014, 08:17 AM   #3
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,176

Rep: Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835
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.
 
  


Reply


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 12:53 PM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 11:48 AM
sed to search and insert column Mecalith Programming 15 02-24-2011 11:52 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 10:14 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 06:42 PM.

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