replace block of text with content of another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace block of text with content of another file
# 1  
Old 05-30-2010
replace block of text with content of another file

Hello,

file1:
Code:
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:
Code:
new text
new text

desired output:
Code:
not to be changed
not to be changed
<start>
new text
new text
<end>
not to be changed
not to be changed

What I have so far:
Code:
sed '/<start>/,/<end>/ {/<start>/b;/<end>/b;d}' file1 | sed '/<start>/r file2'

Works all right but I was expecting to come up with something more straight forward, more terse. Also, this sed solution uses two processes. I couldn't get it work in one.

I tried also awk with getline and a redirection from the file2 but to it was also too complicated. I have that strange feeling that there must be some simple solution but couldn't find it.

What I have so far with awk:
Code:
 awk '!flag{print}flag{getline < "file2";print}/start/{flag=1}/end/{flag=0;print}' file1

output:
Code:
not to be changed
not to be changed
<start>
new text
new text
old stuff
<end>
<end>
not to be changed
not to be changed


Last edited by ripat; 05-30-2010 at 08:35 AM..
# 2  
Old 05-30-2010
Code:
awk '/start/{f=1;print;while (getline < "file2"){print}}/end/{f=0}!f' file1

This User Gave Thanks to danmero For This Post:
# 3  
Old 05-30-2010
Nice! Thank you.

---------- Post updated at 04:02 PM ---------- Previous update was at 02:51 PM ----------

The following also works :

Code:
awk 'NR==FNR{a[NR]=$0;next}/start/{f=1;print;for (i in a) print a[i]}/end/{f=0}!f' file2 file1

I benchmarked the two methods and the "getline / redirection" thing seems to be much faster on large files. Something like 5 times faster on a large file2 file (18.000 lines)
# 4  
Old 05-30-2010
Quote:
Originally Posted by ripat
The following also works :

Code:
awk 'NR==FNR{a[NR]=$0;next}/start/{f=1;print;for (i in a) print a[i]}/end/{f=0}!f' file2 file1

I benchmarked the two methods and the "getline / redirection" thing seems to be much faster on large files. Something like 5 times faster on a large file2 file (18.000 lines)
If all of your replacement lines are identical (as they are in your original post), then yes, it works. However, if the lines in File2 are not identical, and if you wish for their order to be preserved, then you cannot use that approach; you would need to store file2 in a string or use a loop with a counter (c-style for or while), to do it correctly.

Quote:
Originally Posted by http://www.opengroup.org/onlinepubs/9699919799/utilities/awk.html

for (variable in array)
which shall iterate, assigning each index of array to variable in an unspecified order.
Code:
$ jot 5
1
2
3
4
5
$ jot 5 | awk '{a[NR]=$0} END {for (i in a) print a[i]}'
2
3
4
5
1

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace file name with Space as content

Hi, I am having a files in my directory like this: 2014 1049_file1.txt 2014 1050_file2.txt 2014 1110_file3.txt 2014 1145_file4.txt 2014 2049_file5.txt I need to replace the above file names like this without changing the content of filename: file1.txt file2.txt file3.txt... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

2. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

3. 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

4. Shell Programming and Scripting

Replace the content of file with incremented value

I have a file myfile with only one value 1000.I am using it in a shell script.Each time i run the script,the file shud get incremented by 1. I have used the below code for incrementing the value- curr=`cat myfile` echo $curr curr=`expr $curr + 1` But i am not sure how to save this replaced... (2 Replies)
Discussion started by: saga20
2 Replies

5. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

6. Shell Programming and Scripting

Need to replace particular content in a xml file

Hi, My requirement is to find a text and replace it with another in a XML file. I am new to Unix,Please provide some suggestion to achieve. Find: <Style ss:ID="ColumnHeader1"> Replace with: <Style ss:ID="ColumnHeader1"> <Borders> <Border ss:Position="Bottom"... (4 Replies)
Discussion started by: cnraja
4 Replies

7. 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

8. Shell Programming and Scripting

Replace file content after checking value

Our system is receiving one feed from the third party. One of the field in the flat file is ID which id from position 19 to 27. In some cases this ID is coming as 9 zeros (000000000) or 1 right padded zero. ( 0) For these specific records I want to replace fthis field with blank... (3 Replies)
Discussion started by: varunrbs
3 Replies

9. 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

10. Shell Programming and Scripting

SED to replace file content

Hi, I want to replace _F* by _F in a xml file. what is the sed command. I have tried sed "s/_F$/_F/g" or sed "s/_F*/_F/g" , but it does not work. thx file content <TAG>KC_FOU</TAG> <TAG>KC_FABC</TAG> <TAG>KC_FABCDG</TAG> desire output <TAG>KC_F</TAG> <TAG>KC_F</TAG> <TAG>KC_F</TAG> (6 Replies)
Discussion started by: godfreyyip
6 Replies
Login or Register to Ask a Question