Replacing match lines from one file with another file

Please help me in this scenario

i have two text files as below

file1.txt

record_1
conversion of a file;
code change;
data types;
lenght;
end;

record_2
field name;
string;
new fileline;
end;

record_3
address;
street;
zipcode;
state;
end;

file2.txt

record_2
field name;
string;
new fileline;
category;
decimal type;
end;

i want the output be like this in file1.txt

file1.txt

record_1
conversion of a file;
code change;
data types;
lenght;
end;

record_2
field name;
string;
new fileline;
category;
decimal type;
end;

record_3
address;
street;
zipcode;
state;
end;

i want to search for the record_2 pattern in file1 and using file2, if it exist i want to replace the whole line from record_2 to end; with the file2 content in file1. always my search pattern should replace lines between record_? through end; with second file.

please help me to solve this by using shell scripting.

Thanks in Advance,

Welcome to the forum.

Any attempts / ideas / thoughts from your side? Any preferred tools?

I tried using

sed  '/record_2/r file2' file1

but it didn't work ... i'm trying to do this unix

Try

awk 'NR == FNR {T[$1] = $0; next} $1 in T {$0 = T[$1]} 1' RS= ORS="\n\n" file2 file1
1 Like

thanks!!

but this

awk 'NR == FNR {T[$1] = $0; next} $1 in T {$0 = T[$1]} 1' RS= ORS="\n\n" file2 file1

is not working in different scenario if the file has more than 500 lines.

Could you please help me in any other way like in loop condition

What isn't working, could you be more specific?

a) Does it work on your system with the sample given?
b) Does your real file comply to that structure?
c) Up to which record count DOES it work?
d) see post#6

Lets say with the example I have file1:

This is file1 contents:

include "~$XFR/conv.xfr";

/*C1_T0*/
out :: conv_c1_t0(in) =
begin
out.field1 ::decimal_lpad(in..efgh);
out.country2 :: in.country.sub_country;
out.cur :: if(!is_defined(in.tran.ghjk) || is_blank(string_lrtrim(in.tran.ghjk)))
                 string_lpad(" ",6)
                else
                string_repad(in.tran.ghjk,6);
end;

/*C1_T1*/

out :: conv_c1_t1(in) =
begin

out.test ::decimal_lpad(in..test1);
out.zipcode :: in.state.state1;
out.pin :: in.country.state;
out.adr :: in.adr1.adr2;

end;

And file2 contents be like with extra one filed in conv_c1_t1:

out :: conv_c1_t1(in) =
begin

out.test ::decimal_lpad(in..test1);
out.zipcode :: in.state.state1;
out.pin :: in.country.state;
out.adr :: in.adr1.adr2;
out.date :: if(!is_defined(in.temp.date1) || is_blank(string_lrtrim(in.temp.date1)))
                 string_lpad(" ",6)
                else
                string_repad(in.temp.date1,6);
end;

Now My output should be like out :: conv_c1_t0(in) = from file1 and updated function of out :: conv_c1_t1(in) = from file 2 as it contains one extra field :

include "~$XFR/conv.xfr";

/*C1_T0*/
out :: conv_c1_t0(in) =
begin
out.field1 ::decimal_lpad(in..efgh);
out.country2 :: in.country.sub_country;
out.cur :: if(!is_defined(in.tran.ghjk) || is_blank(string_lrtrim(in.tran.ghjk)))
                 string_lpad(" ",6)
                else
                string_repad(in.tran.ghjk,6);
end;

/*C1_T1*/

out :: conv_c1_t1(in) =
begin

out.test ::decimal_lpad(in..test1);
out.zipcode :: in.state.state1;
out.pin :: in.country.state;
out.adr :: in.adr1.adr2;
out.date :: if(!is_defined(in.temp.date1) || is_blank(string_lrtrim(in.temp.date1)))
                 string_lpad(" ",6)
                else
                string_repad(in.temp.date1,6);
end;

Note : File 2 we are generating in our script dynamically.