Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Merging two files and changing format

by webby01 (Initiate)
on Oct 11, 2012 at 14:52 UTC ( #998464=perlquestion: print w/ replies, xml ) Need Help??
webby01 has asked for the wisdom of the Perl Monks concerning the following question:

Comment on Merging two files and changing format
Re: Merging two files and changing format
by choroba (Abbot) on Oct 11, 2012 at 15:13 UTC
    You can read from both input files simlultaneously:
    #!/usr/bin/perl use warnings; use strict; open my $IN1, '<', 'infile_1' or die $!; open my $IN2, '<', 'infile_2' or die $!; open my $OUT, '>', 'test2' or die $!; while (my $in1 = <$IN1>) { chomp $in1; defined(my $in2 = <$IN2>) or die "file2 too short\n"; $in2 =~ s/\s//g; print {$OUT} join("\t", split /\s+/, $in1), "\t", join("\t", split + //, $in2), "\n"; } die "file1 too short\n" if <$IN2>; close $OUT;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      this code gives me a file with 805948 rows, when the desired amount of rows is 660918.

      i think the problem is that some entries are -- instead of, 'AA' for example in the case of missing data. Are there any modifications I can make to the code to remedy this? Thank you.

        Please, show an example of a pair of lines that is not processed correctly.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Merging two files and changing format
by BrowserUk (Pope) on Oct 11, 2012 at 15:53 UTC

    C:\test>head -3 1.dat 01 01 01 01 01 01 02 02 02 02 02 02 03 03 03 03 03 03 C:\test>head -3 2.dat A A A B B B C C C C:\test>perl -nle"printf "%s",join qq[\t],split(' ',$_),do{(my$t=<STDI +N>)=~tr/ //d;split'',$t};" <1.dat 2.dat A A A 0 1 0 1 0 1 + 0 1 0 1 0 1 B B B 0 2 0 2 0 2 + 0 2 0 2 0 2 C C C 0 3 0 3 0 3 + 0 3 0 3 0 3 D D D 0 4 0 4 0 4 + 0 4 0 4 0 4 ...

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2014-08-21 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (136 votes), past polls