Replace the text between two lines with different text


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Replace the text between two lines with different text
# 1  
Old 08-23-2008
Replace the text between two lines with different text

I have a file which contains the following data.

Parameters "CParameters"
BEGIN DSSUBRECORD
Name "TgtDB"
Prompt "TgtDB"
Default "edwdev"
ParamType "0"
ParamLength "0"
ParamScale "0"
END DSSUBRECORD
MetaBag "CMetaProperty"

I need to replace the whole block between Parameters "CParameters"
and
MetaBag "CMetaProperty" with the following content in the second file.

BEGIN DSSUBRECORD
Name "SrcDB"
Prompt "SRcDB"
Default "test"
ParamType "0"
ParamLength "0"
ParamScale "0"
END DSSUBRECORD

Output will be like below.

Parameters "CParameters"
BEGIN DSSUBRECORD
Name "SrcDB"
Prompt "SRcDB"
Default "test"
ParamType "0"
ParamLength "0"
ParamScale "0"
END DSSUBRECORD
MetaBag "CMetaProperty"


Can you pleae help me how can i acheive this in shell scripts.

Thanks
# 2  
Old 08-23-2008
sed -e "s/TgtDB/SrcDB/" -e "s/edwdev/test/" file
# 3  
Old 08-23-2008
Here is the scenario.I have the input like this.I want to replace all lines bet ween WARNING! and IMPRESSION with some other lines from different file.:
Input.

line 1
line 2 Impression xxx
line 3
WARNING!
line 5
line 6 Impression yyy
line 7
IMPRESSION
line 9
line 10 Impression zzz
line 11


Text in other file:

line100
line101
line103
line104
line105
line106
linr107


Output
line 1
line 2 Impression xxx
line 3
WARNING!

line100
line101
line103
line104
line105
line106
linr107
IMPRESSION
line 9
line 10 Impression zzz
line 11


Thanks
# 4  
Old 08-24-2008
This works for me on Ubuntu / GNU sed. With a different sed you might need to tweak it a bit.

Code:
sed -e '/WARNING/rotherfile' -e '/WARNING/,/IMPRESSION/d' file

This expects the replacement data in "otherfile"; add a path name to the file if necessary.

Last edited by era; 08-24-2008 at 12:29 AM.. Reason: Don't look now, but your text is all blue
# 5  
Old 08-24-2008
It is working fine.But it is deleting both WARNING! and IMPRESSION lines.It should not delete these two lines.I need to replace the entire text between these two lines.

Thanks
# 6  
Old 08-24-2008
then just try this..
Quote:
sed -e '/WARNING/r otherfile' -e '/line 5/,/line 7/d' file
# 7  
Old 08-24-2008
Or perhaps like this.

Code:
sed -n -e '1,/WARNING/p' -e '/WARNING/rotherfile' -e '/IMPRESSION/,$p' file

Sorry for missing this part of the requirements.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

4. Shell Programming and Scripting

How to replace a text containing new lines using sed or any other method?

Hi, i want to replace "Hi How are You when did you go to delhi" to "Hi How are you when did you come from delhi" in a file. Any idea how to do it? (2 Replies)
Discussion started by: abhitanshu
2 Replies

5. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

6. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

7. Shell Programming and Scripting

How to replace text in a file with text entered

I am trying to write a shell script that will allow the typing of a value, then using that value to replace data in a text file. I suspect I need sed. The format of the file is: Variable1:Value1 Variable2:Value2 The interaction would be something like: Shell Prompt: "Please enter the... (9 Replies)
Discussion started by: cleanden
9 Replies

8. Shell Programming and Scripting

find text but replace a text beside it

I have an html file that looks like this (this is just a part of the html file): <td colspan="3" rowspan="1" style="text-align: center; background-color: rgb(<!-- IDENTIFIER1 -->51, 255, 51);"><small><!-- IDENTIFIER2 -->UP</small></td> This is to automatically update the status of the... (4 Replies)
Discussion started by: The One
4 Replies

9. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

10. UNIX for Dummies Questions & Answers

how to replace one line in text with several lines

Hi, I need to replace a specific line in a text (eg the line contains the word MYSERVER) with a number of lines (let's say 4) in which the word MYSERVER will be replaced with server1 in the first line server2 in the second line server3 in the 3rd line server4 in the 4th line The original... (1 Reply)
Discussion started by: FunnyCats
1 Replies
Login or Register to Ask a Question