Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I join two files side by side?

by esther (Initiate)
on Dec 06, 2001 at 20:38 UTC ( [id://129969]=perlquestion: print w/replies, xml ) Need Help??

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

I want to create a file XYZ from two other files, XY and Z.

XYZ should have three columns of data. The first two columns come from XY and the third column comes from Z.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I join two files?
by davorg (Chancellor) on Dec 06, 2001 at 21:59 UTC
    print while <>;

    And you call it as:

    ./joinfiles x y > xy

    Originally posted as a Categorized Answer.

Re: How do I join two files side by side?
by jdporter (Paladin) on Oct 31, 2006 at 16:19 UTC
Re: How do I join two files?
by blakem (Monsignor) on Dec 07, 2001 at 01:19 UTC
    how about:
    % perl -pe1 xy.txt z.txt > xyz.txt

    -Blake

      Or even the slightly silly:
      % perl -perl xy.txt z.txt > xyz.txt

      -Blake

Re: How do I join two files?
by Fletch (Bishop) on Dec 06, 2001 at 21:06 UTC

    Of course being this is perlmonks, not catmonks . . .

    open( OUT, ">xyzfile" ) or die "Can't create xyzfile: $!\n"; foreach( qw( xfile yfile zfile ) ){ local( *IN ); open( IN, $_ ) or die "Can't open $_: $!\n"; print OUT while <IN>; close( IN ); }

    Originally posted as a Categorized Answer.

Re: How do I join two files?
by runrig (Abbot) on Dec 07, 2001 at 01:27 UTC
    One possibility:
    use File::Copy; open FH, "> xyz" or die "Write xyz - $!"; copy( $_, \*FH ) for qw( xy z ); close FH;

    Originally posted as a Categorized Answer.

Re: How do I join two files?
by TomK32 (Monk) on Dec 07, 2001 at 17:00 UTC
    a much shorter shell version:
    cat xy z > xyz

    Originally posted as a Categorized Answer.

Re: How do I join two files?
by Anonymous Monk on Dec 17, 2002 at 20:32 UTC
    open INF, "< z.txt" or die; # open for reading open OUTF, ">> xyz.txt" or die; # open for appending print OUTF <INF>; close INF; close OUTF;

    Originally posted as a Categorized Answer.

Re: How do I join two files side by side?
by belg4mit (Prior) on Dec 06, 2001 at 22:46 UTC

    lam does this. It can join any number of files "side by side".

Re: How do I join two files?
by Beatnik (Parson) on Dec 06, 2001 at 20:58 UTC
    in your shell (UNIX) : cat x y > xy Similar will work on Windows. type x y > xyaltho I might be wrong there. It's been quite a while since I did THAT :)
      Hi! Thanks for answering so quickly. The problem is that the outfile should contain three columns. My first file contains two data columns, the second file one column. Unfortunately cat only puts one file after the other and not next to the other. Is there any other way?
        Well, that's more than just a simple join. I doubt that any of the listed solutions provide that kind of functionality. I suggest you post a question in SOPW and provide some data format skeleton.

        Greetz
        Beatnik
        ... Quidquid perl dictum sit, altum viditur.
Re: How do I join two files side by side?
by Mago (Parson) on Dec 13, 2004 at 20:15 UTC
    #!/usr/bin/perl -w use strict; my ($string1, $string2); $/ = undef; open (FILE1, "< x") || die "Cannot open file : $!"; open (FILE2, "< y") || die "Cannot open file : $!"; open (FILE3, "> z") || die "Cannot open file : $!"; $string1 = <FILE1>; $string2 = <FILE2>; print FILE3 join('', $string1, $string2); close (FILE1); close (FILE2); close (FILE3);
      That code will in fact only cat the first line of file x and the first line of file y.

      I wouldn't even use perl for this, but if you must:

      #!/usr/bin/perl -w use strict; my ($string1, $string2); # read files in chunks instead of lines # remove if you realy want to read textfiles # line by line local $/ = \32768; # "block size" open (FILE1, "< x") || die "Cannot open file : $!"; open (FILE2, "< y") || die "Cannot open file : $!"; open (FILE3, "> z") || die "Cannot open file : $!"; # set binmode, so binary files don't get messed # up on systems with text/binary mode files # (like windows or MS-DOS) binmode (FILE1); binmode (FILE2); binmode (FILE3); print FILE3 <FILE1>,<FILE2> or die "Error concatenating: $!"; close FILE1 or die "Error closing x: $!"; close FILE2 or die "Error closing y: $!"; close FILE3 or die "Error closing z: $!";

      update: fixed copy/paste error


      Copy and Paste Error !

      Correct Code:

      #!/usr/bin/perl -w use strict; my ($string1, $string2); $/ = undef; open (FILE1, "< x") || die "Cannot open file : $!"; open (FILE2, "< y") || die "Cannot open file : $!"; open (FILE3, "> z") || die "Cannot open file : $!"; $string1 = <FILE1>; $string2 = <FILE2>; print FILE3 join('', $string1, $string2); close (FILE1); close (FILE2); close (FILE3);
        Updated your answer; does it look ok now? Please /msg QandAEditors if you need changes made.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found