How to replace a string in a file with contents of another file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace a string in a file with contents of another file?
# 1  
Old 07-04-2014
How to replace a string in a file with contents of another file?

Hi,

I have to replace a string in my first file with contents of second file. Second file has 5 lines in it.

file1.txt
Code:
This is the first file.
 LineToBeReplaced
 This is the last line of the first file.

file2.txt
Code:
This is line1 of the second file.
 This is line2 of the second file.
 This is line3 of the second file.
 This is line4 of the second file.
 This is line5 of the second file.

file after script should look like.

Code:
This is the first file.
This is line1 of the second file.
This is line2 of the second file.
This is line3 of the second file.
This is line4 of the second file.
This is line5 of the second file.
This is the last line of the first file.

Please suggest if this can be achieved through shell script or should i check for any other options like perl or other tools.

Thanks,
Vijay.

Last edited by Scrutinizer; 07-05-2014 at 03:58 AM.. Reason: CODE tags
# 2  
Old 07-04-2014
Code:
awk 'NR == FNR {a[FNR] = $0; n++; next}
  /LineToBeReplaced/ {for(i = 1; i <= n; i++) {print a[i]}; next}1' file2.txt file1.txt

---------- Post updated at 10:44 PM ---------- Previous update was at 10:43 PM ----------

Below is the run output
Code:
Srinivas@Snow ~/download/log$ cat file2.txt
This is line1 of the second file.
This is line2 of the second file.
This is line3 of the second file.
This is line4 of the second file.
This is line5 of the second file.
Srinivas@Snow ~/download/log$ cat file1.txt
This is the first file.
LineToBeReplaced
This is the last line of the first file.
Srinivas@Snow ~/download/log$ awk 'NR == FNR {a[FNR] = $0; n++; next}
  /LineToBeReplaced/ {for(i = 1; i <= n; i++) {print a[i]}; next}1' file2.txt file1.txt
This is the first file.
This is line1 of the second file.
This is line2 of the second file.
This is line3 of the second file.
This is line4 of the second file.
This is line5 of the second file.
This is the last line of the first file.
Srinivas@Snow ~/download/log$

# 3  
Old 07-05-2014
Try also
Code:
sed '/Replaced/ {r file2
; d}' file1
This is the first file.
This is line1 of the second file.
This is line2 of the second file.
This is line3 of the second file.
This is line4 of the second file.
This is line5 of the second file.
This is the last line of the first file.

# 4  
Old 07-05-2014
Code:
while IFS= read -r line
  do
  case $line in
    *LineToBeReplaced*) cat file2 ;;
    *) printf "%s\n" "$line"
  esac
done < file1 > newfile

Variation to RudiC's approach:
Code:
sed -n '/LineToBeReplace/!{p;d;}; r file2' file1 > newfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Emergency UNIX and Linux Support

sed replace file contents by reading from another file

Hello, My input file1 is like this by tab-delimited chr1 mm10_knownGene stop_codon 3216022 3216024 0.000000 - . gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; chr1 mm10_knownGene CDS 3216025 3216968 0.000000 - 2 gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; ... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

sed - Replace string with file contents

Hello, I have two files: file1 and file2 file1 has the following info: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP address (1.1.1.1) What I want to do is replace localhost with 1.1.1.1, so that the... (4 Replies)
Discussion started by: Jay Kah
4 Replies

4. Shell Programming and Scripting

Run a program-print parameters to output file-replace op file contents with max 4th col

Hi Friends, This is the only solution to my task. So, any help is highly appreciated. I have a file cat input1.bed chr1 100 200 abc chr1 120 300 def chr1 145 226 ghi chr2 567 600 unix Now, I have another file by name input2.bed (This file is a binary file not readable by the... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

5. Shell Programming and Scripting

Replace Contents between 2 strings in a file with contens of another file

Please I want to replace all the contents beween "Section" and "Ensection" in file1 with all contents in file2. Example: file1: Section "Screen" DefaultDepth 24 SubSection "Display" Depth 8 ViewPort 0 0 Modes "1024x768" "800x600" "640x480" EndSubsection SubSection "Display" Depth... (9 Replies)
Discussion started by: powelltallen
9 Replies

6. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

7. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

8. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

9. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

10. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies
Login or Register to Ask a Question