The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert 2 lines in a file at a specific location potro Shell Programming and Scripting 2 05-08-2008 07:38 PM
Remove duplicates from File from specific location gopikgunda Shell Programming and Scripting 1 04-09-2008 02:16 AM
Insert lines at specific location in file hcclnoodles Shell Programming and Scripting 17 03-28-2007 08:00 AM
excutable script to copy a file to a different location. nazehcalil UNIX for Dummies Questions & Answers 4 12-21-2006 09:17 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 05:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-11-2009
gshepherd7 gshepherd7 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 4
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 (permalink)  
Old 03-11-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,259
Code:
sed '2 r file2' <file1
line 1
line 2
paragraph 1
paragraph 2
line 3
line 4
  #3 (permalink)  
Old 03-11-2009
gshepherd7 gshepherd7 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 4
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 (permalink)  
Old 03-11-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,259
My line works, yours doesn't
Inside single quotes no substituion can occure.

Try
Code:
sed "$POS r ${PACKAGE}.xml" < a_Overview2_New.xml
  #5 (permalink)  
Old 03-11-2009
gshepherd7 gshepherd7 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 4
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 (permalink)  
Old 03-11-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,259
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 09:31 AM.. Reason: typo
  #7 (permalink)  
Old 03-11-2009
gshepherd7 gshepherd7 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 4
Okay...thanks for the help. That's what I ended up starting to do.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0