Replacing match lines from one file with another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing match lines from one file with another file
# 1  
Old 01-17-2018
Replacing match lines from one file with another file

Please help me in this scenario

i have two text files as below

file1.txt

Code:
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

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


i want the output be like this in file1.txt

file1.txt

Code:
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,


Moderator's Comments:
Mod Comment Please use CODE tags (for data as well) as required by forum rules!

Last edited by RudiC; 01-17-2018 at 09:44 AM.. Reason: Added CODE tags. / velraj9: correcting some lines
# 2  
Old 01-17-2018
Welcome to the forum.

Any attempts / ideas / thoughts from your side? Any preferred tools?
# 3  
Old 01-17-2018
I tried using
Code:
sed  '/record_2/r file2' file1

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


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-17-2018 at 03:29 PM.. Reason: Added CODE tags.
# 4  
Old 01-17-2018
Try
Code:
awk 'NR == FNR {T[$1] = $0; next} $1 in T {$0 = T[$1]} 1' RS= ORS="\n\n" file2 file1

This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-18-2018
thanks!!

but this
Code:
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

Last edited by vbe; 01-18-2018 at 04:03 AM.. Reason: code tags please even for so little...
# 6  
Old 01-18-2018
What isn't working, could you be more specific?
# 7  
Old 01-18-2018
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match pattern only between certain lines in entire file

Hello, I have input that looks like this: * 0 -1 103 0 0 m. 7 LineNr 23 ClauseNr 1: 1: 1: 304: 0 0 SentenceNr 13 TxtType: Q Pargr: 2.1 ClType:MSyn PS004,006 ZBX= 0 1 1 0 7 -1 -1 3 2 3 2 -1 1 1 -1 -1 -1 -1 0 501 0 PS004,006 ZBX ... (2 Replies)
Discussion started by: jvoot
2 Replies

2. Shell Programming and Scripting

Matching and Replacing file lines starting with $

Here is the task that I was presented with: I am dealing with about a 10,000 line input deck file for an analysis. About 10 separate blocks of around 25 lines of code each need to be updated in the input deck. The input deck (deckToChange in the code below) comes with 2 separate files. File 1... (5 Replies)
Discussion started by: tiktak292
5 Replies

3. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

4. Shell Programming and Scripting

Replacing a single line with multiple lines in a file

Hi Am confused with the usage of "sed" command I want to replace a single line with multiple lines of a file.. eg., A file has Hi, How are you? I need to replace as Am fine What are You doing? I used the script as string1="Hi, How are you?" echo "$string1 is the value"... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

5. Shell Programming and Scripting

Replacing lines which match certain criteria

Hi, I have code which is like this <TABLE name="UsageDetail_24> <ROW> <Date24><!]></Date24> <Time24><!]></Time24> <Destination24><!]></Destination24> <Rate24><!]></Rate24> <Duration24><!]></Duration24> <Cost24><!]></Cost24> <Allowance24><!]></Allowance24> </ROW> <ROW>... (3 Replies)
Discussion started by: legolad
3 Replies

6. Shell Programming and Scripting

Replacing multiple lines in file from another file

Hi All, I'm trying to parse a XML file and replace few lines of file with text present in another file. But only issue is I need to replace only those lines from XML which are preceeding the JOBNAME =*RRN* For ex test.xml(content of XML file in which I want to replace lines is ) <JOB . . .... (10 Replies)
Discussion started by: techneel
10 Replies

7. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

8. Shell Programming and Scripting

Replacing Block of lines in a text file

Dear All, Regards of the Day. I have a text file with some functions: Function1 { parameter 1 parameter 2 parameter 3 } end Function2 { parameter 1 parameter 2 parameter 3 } (1 Reply)
Discussion started by: ashisharora
1 Replies

9. Shell Programming and Scripting

replacing specific lines in a file

Hi there I have a file which has the lines # Serial number for hostid EXP_SERIAL_="" These lines could be anywhere in the file as far as line numbers go, I would like replace these two lines with # Serial number for hostid $var1 EXP_SERIAL_$var1="$var2" Is there a quick and simple... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. UNIX for Advanced & Expert Users

replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file? see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can,... (3 Replies)
Discussion started by: Terrible
3 Replies
Login or Register to Ask a Question