Replace Block


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace Block
# 1  
Old 05-09-2010
Replace Block

I'd like to replace content within the markers "(div id=m1)" and "(/div)". Between the markers there are several lines of text but no nested brackets (so no parser is required).

In the beginning I thought this to be an easy task like:
Code:
"sed s/(div id=m1).*(/div)/(div id=m1)$replacetextfromfile(/div)/"

But I failed (eg. sed's greedyness, mulitline problem; i tried preprocessors like Gema. Is it really required to write an awk script?).

Question: is there a straightforward way to replace text between some xml-brackets by text read from file?

Thanks!

Last edited by Franklin52; 05-09-2010 at 11:29 AM.. Reason: Please use code tags!
# 2  
Old 05-09-2010
If your replacement text contains the same character you are using as a delimiter, you need to escape it. In the example you provided, you included a closing DIV element, i.e. </div>. Try using <\/div> instead.

I have used multi-line replacements with sed, but I tend to use single quotes and break out when I need to insert variables. For example,
Code:
var=Search
sed 's/searchText/new'${var}'Text/'

# 3  
Old 05-09-2010
Thanks for your comments. But the main problem: how do you define "searchText"? It has to be a non-greedy regular expression spanning several lines because the text between both delimiters does not matter.
# 4  
Old 05-09-2010
# 5  
Old 05-10-2010
Unfortunately no solution there.

Perhaps there are more tipps how to
* replace a block of multiline text, surrounded by xml-brackets?
# 6  
Old 05-10-2010
MySQL

Quote:
Originally Posted by Dieter5
Unfortunately no solution there.

Perhaps there are more tipps how to
* replace a block of multiline text, surrounded by xml-brackets?
Hi

For example let my file is like below Smilie

Code:
[root@rhnserver ~]# cat testfile
......................
............................
................................
"(div idm1)"
The first line
The second line
This third test
This fourth test
The last line
"(/div)"
......................
............................
................................

I take line number between "(div idm1)" and "(/div)"
My goal is learn the how many line to delete..

Code:
linecount=$( (sed -n '/"(div idm1)"/,/"(\/div)"/{;/"(div idm1)"/!p;}' testfile | sed -n '$!p' | wc -l) )

Now I remove between lines..

Code:
while [ $(( linecount -= 1 )) -gt -1 ]
do
sed -i '/^"(div idm1)"/ {n;d;q}' testfile
done


And let the other file is look like

Code:
[root@rhnserver ~]# cat file_to_add
test
test12
deneme
test123

Finally add to lines between "(div idm1)" and "(/div)" from "file_to_add" file.

Code:
#Add Other File to My Org File
while read line
   do
     sed -i "/^\"(\/div)\"/ i$line" testfile
   done < file_to_add
 
[root@rhnserver ~]# cat testfile
......................
............................
................................
"(div idm1)"
test
test12
deneme
test123
"(/div)"
......................
............................
................................


I escape Smilie I shall sleep Smilie

Regards
ygemici

Last edited by Scott; 05-11-2010 at 02:29 PM.. Reason: Added more code tags
# 7  
Old 05-10-2010
Hello, Dieter5, and welcome to the forums.

Writing a sed script to replace a multi-line span of text which is terminated by a sequence of characters which does not occur within the text to be replaced is easy. The replacement text may be the more complicated part. The replacement text cannot contain any characters that sed treats specially in replacement text, such as an ampersand, a backslash, the s command's delimiter, and perhaps others that i'm overlooking at this moment. Is it possible that the replacement text could contain any such characters? If so, then it will need to be escaped properly.

If you would like me to spend some of my time putting together a possible solution, please answer those questions and provide some sample input (including replacement text) and the desired output, so that I can test without having to contrive input data which may not match your requirements.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Bash shell: Replace a text block by another

Hello, I'm an starter in Bash scripting. I would like to write a script in Bash shell that replaces a specific text block (a function) by another text block in a file: for example in my file --> $HOME/myFile.js replacing following function between other functions in the file: function ABC()... (6 Replies)
Discussion started by: om1559
6 Replies

3. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

4. Shell Programming and Scripting

using sed/awk to replace a block of text in a file?

My apologies if this has been answered in a previous post. I've been doing a lot of searching, but I haven't been able to find what I was looking for. Specifically, I am wondering if I can utilize sed and/or awk to locate two strings in a file, and replace everything between those two strings... (12 Replies)
Discussion started by: kiddsupreme
12 Replies

5. Shell Programming and Scripting

replace block of text with content of another file

Hello, file1: not to be changed not to be changed <start> old stuff old stuff old stuff <end> not to be changed not to be changed file2: new text new text desired output: (3 Replies)
Discussion started by: ripat
3 Replies

6. Shell Programming and Scripting

Replace text block in multiple files

I need to replace (delete) a text block in a bunch of files, its a html table, almost at the end of pages but the location varies. In Windows I used Filemonkey, but nothing like that in Unix? There is replace from mysql, but how does it deal with newlines? sed only works with single lines,... (6 Replies)
Discussion started by: eiland
6 Replies

7. Shell Programming and Scripting

Need to find a multiple line block and replace with a multiple line block

I would perfer to use cut and paste to do this but I can't find a GUI to do this with. What I want to do is to find a multiple line block of code like Exit Sub Log_Handler: then replace it with GoTo RSLogRtn Exit Sub Log_Handler: Basically it is just an insert, but I may want to... (8 Replies)
Discussion started by: Randem
8 Replies

8. Shell Programming and Scripting

finding a block in a file and replace with another file block.

(1) Yes but how is this block different from the other 24? You will need this information in order to identify and replace this block correctly (out of the 25). Ans: The 1st line and last line of this block are unique from other block. The 1st line is “rem Subset Rows (&&tempName.*) and The... (1 Reply)
Discussion started by: Zaheer.mic
1 Replies

9. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

10. UNIX for Dummies Questions & Answers

Search/replace using visual block

Hi, how would we replace a few patterns on the same line with a change of it's own..using visual block on vim editor. ie a file contains lines such as the following: abccss (dfrss)) emailkk abdcss (dfrss)) dmailkk Using visual block once, replacement is indeed to get the following output:... (0 Replies)
Discussion started by: Manan
0 Replies
Login or Register to Ask a Question