Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

I need to append to text file

by aatlamaz (Novice)
on Jan 18, 2005 at 17:28 UTC ( [id://423102]=perlquestion: print w/replies, xml ) Need Help??

aatlamaz has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I know this must be very easy but I am new and learning Perl. I have two text files that need to be appended. I tried to solve with DOS command copy text1 + text2, but I couldn't handle newline problem. Is there a way to do this in Perl. Thanks so much

Replies are listed 'Best First'.
Re: I need to append to text file
by Grygonos (Chaplain) on Jan 18, 2005 at 17:38 UTC

    Sounds like you really want to merge two files..based on your copy text1 + text2 example

    open(TEXT1,">>text1.txt") or die "$!"; open(TEXT2,"<text2.txt") or die "$!"; while(<TEXT2>) { print TEXT1 $_; } close(TEXT2); close(TEXT1);
Re: I need to append to text file
by g0n (Priest) on Jan 18, 2005 at 17:40 UTC
    open (IFILE1,"Filename1.txt") or die "Can't open file1";
    open (IFILE2,"Filename2.txt") or die "Can't open file2";
    open (OUTFILE,">OutputFile.txt") or die "Can't open file for writing";
    
    while (<FILE1>){print OUTFILE $_}
    while (<FILE2>){print OUTFILE $_}
    
    close IFILE1;
    close IFILE2;
    close OUTFILE;
    
    A bit long winded, theres probably a one liner to do it, but it'll do the job, and might help to explain file handling in perl into the bargain!

    Should also have said that this combines them into a third file - a nice cautious option!

      your code is certainly correct (even if it doesn´t use strict;), but aatlamaz states that he couldn't handle newline problem. That leads me to the assumption, that at least one of the files has no newline at its last line. Thus it must be added.
      So i changed above code to handle that (while also adding argument handling and support for more than two files):
      use strict; my $outfile = pop @ARGV or die "No outfile!\n"; my @infiles = @ARGV or die "No infile(s)!\n"; open OUTFILE, ">$outfile" or die "Cannot open $outfile!\n"; for ( @infiles ) { open (INFILE, $_) or die "Can't open $_!\n"; while ( <INFILE> ) { chomp; print OUTFILE "$_\n"; } close INFILE; } close OUTFILE;


      The script can be called like this:
      shell: perl script.pl file1 file2 fileN outfile

      holli, regexed monk
Re: I need to append to text file
by Ven'Tatsu (Deacon) on Jan 18, 2005 at 18:00 UTC
    The no code solution: perl -pe 1 file1.txt file2.txt > out.txt
      Appreciate you all.
Re: I need to append to text file
by amw1 (Friar) on Jan 18, 2005 at 17:41 UTC
    untested
    use strict; my $file1 = shift(@ARGV); my $file2 = shift(@ARGV); # open the first file, slurp the contents into # $file1_contents open(FILE1, "<", $file1) or die "Whoops!"; my $file1_contents = <FILE1>; close(FILE1); # open the second file in append mode, print # the contents from file1 into file2 open(FILE2, ">>", $file2) or die "Whoops!"; print FILE2 $file1_contents; close(FILE2);
    This is not tested so I may have screwed something up but the idea should work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://423102]
Approved by kutsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found