develooper Front page | perl.beginners | Postings from January 2002

Re: How to abut two files?

Thread Previous | Thread Next
From:
Jeff 'japhy' Pinyan
Date:
January 30, 2002 16:15
Subject:
Re: How to abut two files?
Message ID:
Pine.GSO.4.21.0201301913050.27903-100000@crusoe.crusoe.net
On Jan 30, Catherine L Harris said:

>One of the most common needs in data analysis is to "abut" two files.  This
>means to create a single file by horizontally pasting two files together.

If you have Perl, the following program does the job:

  #!/usr/bin/perl -w

  die "usage: $0 file1 file2 > output\n" unless @ARGV == 2;

  open LEFT, $ARGV[0] or die "can't read $ARGV[0]: $!";
  open RIGHT, $ARGV[1] or die "can't read $ARGV[1]: $!";

  while (<LEFT>) {
    chomp;
    print $_, scalar <RIGHT>;
  }

  close LEFT;
  close RIGHT;

This of course assumes that the files are the same number of lines.

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About