LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-08-2009, 05:02 PM   #1
hal8000b
Member
 
Registered: Mar 2001
Location: UK
Distribution: Mint, Arch, Debian7
Posts: 170

Rep: Reputation: 21
Inserting Multiple Lines (with newline) using sed or awk


So far, I've only done basic substitution with sed, so dont know if this is possible.

I need to replace part of a line in a file with multiple lines, however need to separate each line with a newline is this possible?

Example, need to remove line 2
</head>
<h1>My Blog</h1>

and insert line similar to below:
--snip
</head>
<body background="gifs/blue.gif">
<table class="table-head">
<tr>
<td><img src="jpgs/image_a.jpg" border="0"></td>
<td valign="middle" align="center" style="font-size: 40px; color:red">My Blog</td>
<td><img src="jpgs/image_b.jpg" align="right" border="0"></td>
</tr>

If the multiple lines were part of a text file, would inserting a text file automatically include newline characters?


Thanks in advance to anyone who can help?
 
Old 03-08-2009, 05:21 PM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Put the lines you want to add into another file, and then feed that into your sed command using the r filename syntax.

Here's an example:
Code:
telemachus ~/practice $ cat practice 
foo
bar
baz
telemachus ~/practice $ cat file 
cats
love
dogs
telemachus ~/practice $ sed '/foo/ r file' < practice 
foo
cats
love
dogs
bar
baz
See here for more: http://www.grymoire.com/Unix/Sed.html#uh-37
 
  


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
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Inserting spaces using SED/AWK ? aravindts Programming 4 09-29-2006 05:28 AM
find awk sed.. something along these lines citrus Linux - General 1 08-21-2006 03:04 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM


All times are GMT -5. The time now is 04:09 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration