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. | [reply] [d/l] [select] |
Re: How do I join two files side by side?
by jdporter (Paladin) on Oct 31, 2006 at 16:19 UTC
|
| [reply] |
Re: How do I join two files?
by blakem (Monsignor) on Dec 07, 2001 at 01:19 UTC
|
% perl -pe1 xy.txt z.txt > xyz.txt
-Blake | [reply] [d/l] |
|
Or even the slightly silly:
% perl -perl xy.txt z.txt > xyz.txt
-Blake
| [reply] [d/l] |
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. | [reply] [d/l] |
Re: How do I join two files?
by runrig (Abbot) on Dec 07, 2001 at 01:27 UTC
|
use File::Copy;
open FH, "> xyz" or die "Write xyz - $!";
copy( $_, \*FH ) for qw( xy z );
close FH;
Originally posted as a Categorized Answer. | [reply] [d/l] |
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. | [reply] [d/l] |
Re: How do I join two files side by side?
by belg4mit (Prior) on Dec 06, 2001 at 22:46 UTC
|
| [reply] |
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. | [reply] [d/l] |
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 :) | [reply] [d/l] [select] |
|
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?
| [reply] |
|
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.
| [reply] |
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);
| [reply] [d/l] |
|
#!/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
| [reply] [d/l] |
|
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);
| [reply] [d/l] |
|
Updated your answer; does it look ok now? Please /msg QandAEditors if you need changes made.
| [reply] |
|