script to combine colums from multiple files
Ian Petersen
ispeters-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Fri Sep 12 15:08:50 UTC 2008
On Fri, Sep 12, 2008 at 11:03 AM, Madison Kelly <linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org> wrote:
> In perl, I might do something like:
>
> -=-=-=-=-=-
> my %cols=();
> my @lines=();
> open(READ, "</path/to/file.txt") or die "Can't read... $!\n";
> while (<READ>)
> {
> chomp;
> my @cols=split/,/;
> # Grab the first and third column.
> push @{$cols{a}}, $cols[0];
> push @{$cols{b}}, $cols[2];
> }
> close READ;
>
> open(READ, "</path/to/file2.txt") or die "Can't read... $!\n";
> while (<READ>)
> {
> chomp;
> my @cols=split/,/;
> # Grab the second and fourth column.
> push @{$cols{c}}, $cols[1];
> push @{$cols{d}}, $cols[3];
> }
> close READ;
>
> open(WRITE, "</path/to/file2.txt") or die "Can't read... $!\n";
> foreach my $line (@lines)
> {
> # $col will be an array ref
> print WRITE "${$cols{a}}, ${$cols{b}}, ${$cols{c}}, ${$cols{d}}\n";
> }
>
> exit 0;
> -=-=-=-=-=-
>
> Note that I've not run this so there may be errors, but hopefully it will
> get you started.
I'm not much of a Perl hacker, so I could be wrong, but I think Madi
made a copy-and-paste error in the last call to open. I think you
want
open(WRITE, ">/path/to/file2.txt") or die "Can't write... $!\n";
The most important change being the '>', and not the error message.
Ian
--
The Toronto Linux Users Group. Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
More information about the Legacy
mailing list