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

Re: combining 2 files with a comon field

by mattk (Pilgrim)
on May 18, 2005 at 13:03 UTC ( #458182=note: print w/replies, xml ) Need Help??

in reply to combining 2 files with a comon field

This is assuming the files have the same amount of lines:
use IO::File; $f1 = new IO::File "< file1"; $f2 = new IO::File "< file2"; $f3 = new IO::File "> file3"; while (my ($c1, $c2) = map { m/^.*?\|(.*)\|$/ } ($f1->getline, $f2->ge +tline)) { print $f3 "A$.|$c1|$c2|\n"; last if eof; }
Reads in one line from each file, extracts the column values using map and a regex, and then prints out a new line containing both column values, plus a header made from $.

Replies are listed 'Best First'.
Re^2: combining 2 files with a comon field
by jhourcle (Prior) on May 18, 2005 at 13:57 UTC

    If we're going with that assumption (same number of lines, and the keys are in the same order), then it's a one liner in unix shell:

    paste  -d\| file1 file2 | cut -d\| -f1,2,5- > file3

    I think I prefer jmcnamara's solution with join, though, as it's more forgiving of bad input.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://458182]
[1nickt]: Sad that so much of our humour these days is dry, wry, gallows humor :-/
[1nickt]: One of the great human characteristics .... to laugh in the face of doom :-)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2017-03-26 18:57 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (315 votes). Check out past polls.