LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
LinkBack Search this Thread
Old 05-21-2007, 04:29 PM   #1
Namachivayam
LQ Newbie
 
Registered: May 2007
Posts: 1

Rep: Reputation: 0
Replacing a bunch of lines between a pattern


Hi,
I have two files :

File 1 :

<HEAD>
....
....
</HEAD>


File 2 :

echo 1
echo 2

That you can write as one

echo 1; echo 2

or

echo 1 && echo 2



The question is :
How can I replace content of the File2 to the File1 between <HEAD> and </HEAD>

Thanks.
Namachi
 
Old 05-21-2007, 07:23 PM   #2
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
It isn't clear to me what you want to do. Do you want the contents in file 1, between the <HEAD> and </HEAD> tags to be deleted and replaced with the contents of file2.

You could do the deletion in sed pretty easily:
Code:
$ sed '/<head>/,/<\/head>/{
/<head>/n;
/<\/head>/n;
d }' file1.htm
I saved this page and ran the above command in cygwin.
Here are the first 5 lines of the result:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="en">
<head>
</head>
<!-- logo -->
If the <head> and </head> tags might not be alone on the same line, you will need to adjust the sed command.

For example, for the line containing the <head> tag:
/<head>/s/^\(.*\)<head>.*/\1<head>/n;
This will preserve what came before <head>, deleting what came after. It will fail if the pattern is:
something .... <head> more junk ... </head> more junk.

Because what you seam to be doing is creating an html document out of a template, I would recommend that instead you break the file1 template into two pieces and instead either cat the three files together, or use a here document.
Code:
cat >file.htm <<- "HEAD"
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html dir="ltr" lang="en">
     <head>
HEAD
cat >>file.htm <file2
cat >>file.htm <<- "TAIL"
     </head>
     </html>
TAIL
or

Code:
cat head >file.htm 
cat file2 >>file.htm
cat tail >>file.htm

Last edited by jschiwal; 05-21-2007 at 07:25 PM.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a image filename with a regex pattern in a script jonaster Linux - General 1 09-05-2006 07:02 PM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 11:08 AM
Replacing new lines (\n) from a file bkeeper Linux - Software 4 12-15-2005 02:13 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM
replacing pattern with sed produces double realos Programming 1 10-17-2002 08:03 PM


All times are GMT -5. The time now is 05:13 AM.

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
Open Source Consulting | Domain Registration