Connecting Tech Pros Worldwide Forums | Help | Site Map

append file to file

Newbie
 
Join Date: Oct 2007
Posts: 11
#1: Oct 16 '07
i want to append a file(say g1) and add info frm temp file (say g2) .... the info frm g2 must be added at a paticular line of the file g1 say frm third line .... can anyone help me in this....

Newbie
 
Join Date: Oct 2007
Posts: 11
#2: Oct 16 '07

re: append file to file


Expand|Select|Wrap|Line Numbers
  1. my $filec="g32.pl";
  2. my $temp="g31.pl";
  3.  
  4. open(INFO, ">>$filec");
  5. open(TEMP, "<$temp");
  6.  
  7. while(<TEMP>)
  8. {
  9.     print INFO ("$_");
  10. }
  11.  
  12. close(INFO);
  13. close(TEMP);
  14.  
this writes it to the end but my file g32 isa big file and i wanna add the content frm g1 at a specific location
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Oct 16 '07

re: append file to file


use the Tie::File module

perldoc: Tie::File
Newbie
 
Join Date: Oct 2007
Posts: 11
#4: Oct 17 '07

re: append file to file


hey dint understand how to use that module exactly i am new to perl......
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#5: Oct 17 '07

re: append file to file


OK, but your original question is too vague for me to offer any suggestions. Explain your question better and maybe I can offer a better suggestion.
Newbie
 
Join Date: Oct 2007
Posts: 11
#6: Oct 17 '07

re: append file to file


see i have a file which has a set of lines say
agshdjkhkayd
ahskhduydaiskdha
hdlkhadkausyd
and there is another file which is very complex .....
nw i have to add these three lines of tht file to the tenth line (say) of the complex file.... i have to append file to file but i have to add the lines not at the end but a particular line of the file....hope u understand wat i mean...:)
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#7: Oct 18 '07

re: append file to file


Perhaps Data::Locations

However, it may not be the easiest to learn.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#8: Oct 18 '07

re: append file to file


Quote:

Originally Posted by gayathrib1

see i have a file which has a set of lines say
agshdjkhkayd
ahskhduydaiskdha
hdlkhadkausyd
and there is another file which is very complex .....
nw i have to add these three lines of tht file to the tenth line (say) of the complex file.... i have to append file to file but i have to add the lines not at the end but a particular line of the file....hope u understand wat i mean...:)

Do you mean insert the lines? How do you know which line you have to add the data to? Is it always the same line? Does it overwrite the existing lines or insert the lines?
Newbie
 
Join Date: Oct 2007
Posts: 11
#9: Oct 18 '07

re: append file to file


ya i have to add the three lines to another file and these lines will be constant at least at present i will be working on constant lines and we have to add these three lines at a particular location of the another file (the big file tht i was talking abt)....
Newbie
 
Join Date: Oct 2007
Posts: 11
#10: Oct 18 '07

re: append file to file


the line wher i have to add i will know in advance and obviously it shouldnt overwrite the file to which it is written it should be just added ...
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#11: Oct 18 '07

re: append file to file


untested code but should work:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my $input_file = 'your_file';
  4. my $big_file = 'your_big_file';
  5. my $line_num = 10;
  6. {
  7.    open my $in, $input_file or die "Can't open $input_file: $!"; 
  8.    local @ARGV = ($big_file);
  9.    local $^I = '.bac';
  10.    while (<>) {
  11.       if ($. == $line_num) {
  12.          while (defined (my $input = <$in>)) {
  13.             print $input;
  14.          }
  15.       }
  16.       else {
  17.          print;
  18.       }
  19.    }
  20.    close $in;
  21. }
  22.  
$line_num is the line number of the big fie you want to start the insertion of the lines from the other file.
Newbie
 
Join Date: Oct 2007
Posts: 11
#12: Oct 18 '07

re: append file to file


hey thanks a lott... sme qs ...wat is my $in ...and there s no need rite creating tht .bac file.... srry but i m new to programming ...
Newbie
 
Join Date: Oct 2007
Posts: 11
#13: Oct 18 '07

re: append file to file


and one more thing if u use argv and <> then .... the first line is lost rite ...so by that way we are losing a line ...the first line of the big file frm wher we start
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#14: Oct 18 '07

re: append file to file


Quote:

Originally Posted by gayathrib1

hey thanks a lott... sme qs ...wat is my $in ...and there s no need rite creating tht .bac file.... srry but i m new to programming ...

$in is a lexical filehandle, it is the same as:

open IN, 'filename';


but has advantages over using bareword filehandles like IN. You can use the old style filehandle if you prefer.

if you don't want a backup file created and you are using a non-windows operating system you can try:

$^I = '';


If you are using windows you have to create a backup file.
Newbie
 
Join Date: Oct 2007
Posts: 11
#15: Oct 18 '07

re: append file to file


hey one line is getting cut that is if we start the inserting frm 10th line the tenth line of the big file isnt there in the output ....
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#16: Oct 18 '07

re: append file to file


Quote:

Originally Posted by gayathrib1

hey one line is getting cut that is if we start the inserting frm 10th line the tenth line of the big file isnt there in the output ....


Yes, I see now that will happen. Change these lines:

Expand|Select|Wrap|Line Numbers
  1. else {
  2.    print;
  3. }
  4.  
to just:

Expand|Select|Wrap|Line Numbers
  1. print;
and all should be well.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#17: Oct 18 '07

re: append file to file


Quote:

Originally Posted by gayathrib1

and one more thing if u use argv and <> then .... the first line is lost rite ...so by that way we are losing a line ...the first line of the big file frm wher we start

No, it was lost because of the way I coded the script. See above post.

I am having a hard time reading your posts. Please use correct grammar and spelling from now on. Thanks.
Newbie
 
Join Date: Oct 2007
Posts: 11
#18: Oct 19 '07

re: append file to file


i am sorry for the grammatical errors .... will take care of it henceforth.... thanks i will change the script accordingly and tell you if i have a problem.... thanks again.....
Reply