Append file contents of one file into another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append file contents of one file into another
# 1  
Old 10-06-2015
Append file contents of one file into another

Hello there,

I have a files f1.html, f2.html.

I have a final file called final.html which is something like the below snippet

Code:
<!DOCTYPE html>
<html>
<body>
<h1 align="center">aaaaa Report</h1>
<pppp><p><b>pppp Repository</b></p>
</pppp>
<qqqq><p><b>qqqq Repository </b></p>
</qqqq>
</body>
</html>

I wish to add the entire contents of file1.html at the position "pppp repository" and file2.html at "qqqq repository".

Such that the file final.html looks like

Code:
 
 
<!DOCTYPE html>
<html>
<body>
<h1 align="center">aaaaa Report</h1>
<pppp><p><b>pppp Repository</b></p>
 
file1.html contents
 
</pppp>
<qqqq><p><b>qqqq Repository </b></p>
 
file2.html contents
 
</qqqq>
</body>
</html>


Could you please help me in doing this

Thanks,
Haider

Last edited by H squared; 10-06-2015 at 12:10 PM.. Reason: changed names
# 2  
Old 10-06-2015
Any attempt from your side?

---------- Post updated at 17:26 ---------- Previous update was at 17:22 ----------

Howsoever, try
Code:
sed -e '/pppp Repository/r file1' -e'/qqqq Repository/ r file2' file
<!DOCTYPE html>
<html>
<body>
<h1 align="center">aaaaa Report</h1>
<pppp><p><b>pppp Repository</b></p>
file1.html contents
</pppp>
<qqqq><p><b>qqqq Repository </b></p>
file2.html contents
</qqqq>
</body>
</html>

# 3  
Old 10-07-2015
Hello,

Thank you for the pointer.

I have tried the following command :

Code:
 
sed -e '/pppp Repository/r file1.html' -e'/qqqq Repository/ r file2.html' final1.html


All 3 files : file1.html,file2.html and final1.html are in the same directory.

However, the file final.html is not displaying the added contents of file1.html and file2.html. Instead, the output is being show on the command line.

I have made a modification as :

Code:
 
sed -e '/pppp Repository/r file1.html' -e'/qqqq Repository/ r file2.html' final1.html | tee final.html

Although the file final.html is having the contents of file1.html,file2.html and final1.html, the entire output is being displayed in the command prompt.


Code:
 
<!DOCTYPE html>
<html>
<body>
<h1 align="center">aaaaa Report</h1>
<pppp><p><b>pppp Repository</b></p>
file1.html contents
</pppp>
<qqqq><p><b>qqqq Repository </b></p>
file2.html contents
</qqqq>
</body>
</html>

Please let me know a way to suppress this from being displayed in the command prompt.

Thanks,
Haider
# 4  
Old 10-07-2015
Don't pipe through tee but redirect using > .
This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-07-2015
Quote:
Originally Posted by RudiC
Don't pipe through tee but redirect using > .
Just note that you can't redirect the output of your script to one of your input files.

Redirect your script's output to a temp file and then, if the script completed successfully:
Code:
mv TmpFile Destination

(if there can only be one link to the desired Destination file) or:
Code:
cp TmpFile Destination; rm -f TmpFile

if there might be more than one link to the Destination file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

2. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

3. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

4. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

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

6. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

7. UNIX for Dummies Questions & Answers

Script to append contents of one file to another

Hi, I want to write a script that will write a couple of lines into a file, including the output of another file... Can anyone help? Scenario Day 1 File 1 Contents --> Japan File 2 contents (to be wriiten by the script) --> Name: Sameer Country: Japan Day 1 File 1... (0 Replies)
Discussion started by: carlos_anubis
0 Replies

8. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

9. Shell Programming and Scripting

how to append file contents at server1 to another file in server2

hi is there any way to append the file contents at server1 to another file in server2 by using scp. right now am transferring files to the server2 and using cat command for appending... (2 Replies)
Discussion started by: aemunathan
2 Replies

10. UNIX for Dummies Questions & Answers

append file name contents to a target file

Hi , I want to append the contents of many files (not knowing exactly how many files, i will get) in to one target file on a day today basis. The file names are similar except last value (which is sequence number) eg) file1_20061026_1 file1_20061026_2 etc., How can i read all the files... (2 Replies)
Discussion started by: gopskrish
2 Replies
Login or Register to Ask a Question