Bash copy file contents into an existing file at a specific location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash copy file contents into an existing file at a specific location
# 1  
Old 03-11-2009
Bash copy file contents into an existing file at a specific location

Hi all

I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far:

File A
line 1
line 2
line 3
line 4

File B
paragraph 1
paragraph 2

So I would end up with:
File A
line 1
line 2
paragraph 1
paragraph 2
line 3
line 4

Here's what I have been trying to do:

while read line
do
sed '$POSa\$line' B.txt > A.txt
done < B.txt


Thanks for the help.
# 2  
Old 03-11-2009
Code:
sed '2 r file2' <file1
line 1
line 2
paragraph 1
paragraph 2
line 3
line 4

# 3  
Old 03-11-2009
I tried that and I get the following error:

sed: -e expression #1, char 3: extra characters after command

Here's the line I tried:

sed '$POS r ${PACKAGE}.xml' < a_Overview2_New.xml
# 4  
Old 03-11-2009
My line works, yours doesn't Smilie Smilie
Inside single quotes no substituion can occure.

Try
Code:
sed "$POS r ${PACKAGE}.xml" < a_Overview2_New.xml

# 5  
Old 03-11-2009
My bad on the quotes... When I run it, it does what I expect, however my a_Overview2_New.xml file is now empty. I want that file to contain:

line 1
line 2
para 1
para 2
line 3
line 4
# 6  
Old 03-11-2009
You cannot read a file and overwrite it in instantly. Just redirect output to a temporary file with ">" and just rename it to the original with "mv".

Last edited by zaxxon; 03-11-2009 at 10:31 AM.. Reason: typo
# 7  
Old 03-11-2009
Okay...thanks for the help. That's what I ended up starting to do.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

2. Shell Programming and Scripting

Find and Copy file of specific location

Dear All, I need to transfer all files present in one location to another but those files should be of specific extension like. Find and copy all files of extension .xls, .pdf, .txt from location usr/tmp to location /per/Treat (6 Replies)
Discussion started by: yadavricky
6 Replies

3. Shell Programming and Scripting

Copy file from UNIX to shared location

Hi All, I want to transfer file from UNIX to shared locataion . Shared location doesn't resides on my system. Can somebody tell me is there any way i can transfer file from UNIX to shared location without using any tool WINSCP. Thanks, Amit (1 Reply)
Discussion started by: Amit786
1 Replies

4. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

5. Shell Programming and Scripting

How to copy a file from one location to another location?

I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt. Please help me how to do this in shell script. (1 Reply)
Discussion started by: vel4ever
1 Replies

6. Shell Programming and Scripting

.sh file To rename existing file and copy new file

Hi All, I am very new to shell scripting . In my current task i want to create .sh file that will rename the existing file with appending _bu in it. And then copy new file . e.g if i have file linuxFirst.java then i want to rename it to linuxFirst_bu.java ..Then want replace with latest... (1 Reply)
Discussion started by: maheshkaranjkar
1 Replies

7. UNIX for Dummies Questions & Answers

how to copy a file without remove the contents of the target file?

Hello every body, Kindly support me to "copy a file without remove the contents of the target file" Thanks in advance. :) Ahmed Amer Cairo,Egypt (2 Replies)
Discussion started by: ahmedamer12
2 Replies

8. Shell Programming and Scripting

tail copy of a file to remote location

Hello, I have a solaris box and a windows server. The windows server runs cygwin for ssh service. I have an audit log in solaris box. When ever new records are added to the log file, this delta has to be trasported to a remote file in windows. I can do a ssh once in a while, but want the... (7 Replies)
Discussion started by: unori
7 Replies

9. UNIX for Dummies Questions & Answers

excutable script to copy a file to a different location.

Hi, I'm try to create an executable file to copy a file to a different location. Help plz. Thanx. (4 Replies)
Discussion started by: nazehcalil
4 Replies

10. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies
Login or Register to Ask a Question