Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > merging two files

Reply
Thread Tools

merging two files

 
 
Vahid
Guest
Posts: n/a
 
      06-05-2007
Hi all,
Is it possible to parse two colon delimited files and merge one field
from one of the files with the other file? For example if I want to
merge the second field (b) of the first file with the contents of
a:b:c:d with the third field of the second file with the contents of
e:f:g:h, I would get e:f:b:h. I came up with the following but don't
know where to add the parser for the second file?

sub mkShadow() {
open (SHADOWfh, "<$SHADOW") || die "Can not open the file:
$SHADOW. $!\n";
open (PASSWDfh, "<$PASSWD") || die "Can not open the file:
$PASSWD. $!\n";
open (XSHADOWfh, ">$XSHADOW") || die "Can not open the file:
$XSHADOW. $!\n";

@lines = <SHADOWfh> ;
### @linesX = <XSHADOWfh> # this is what I want
foreach $line (@lines) {
## what goes here?
# Skip comments
next if (m/^#/);
# Skip blank lines
next if (m/^\s*$/);
($username,$pass,$lastchange) = split /:/, $line;
## what goes here?
$x_lastupdate = ($lastchange*86400);
#
print $XSHADOWfh ("username: %s password: %s lastupdate: %d
\n", \
$username,$pass,$x_lastupdate) ;
} # for each
close $SHADOWfh;
close $XSHADOWfh;
close $PASSWDfh;
@args = ("pwdck", "-y", "ALL");
system(@args) == 0 or warn "System @args failed: $?";
}

Thank you very much,

 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      06-05-2007
) = split /:/, $line;Vahid wrote:
> Hi all,
> Is it possible to parse two colon delimited files and merge one field
> from one of the files with the other file? For example if I want to
> merge the second field (b) of the first file with the contents of
> a:b:c:d with the third field of the second file with the contents of
> e:f:g:h, I would get e:f:b:h. I came up with the following but don't
> know where to add the parser for the second file?


First, determine what column is the 'key' for each file.

Read file 1, 'split' each line and create a hash with the value of the
'b' column and the key being the common column in both files.

While reading file 2, 'split' the line. Based on the 'key' column,
use the value from file1 for the 'g' column, and write it to your
new file.


use strict;
use warnings;
>
> sub mkShadow() {
> open (SHADOWfh, "<$SHADOW") || die "Can not open the file:
> $SHADOW. $!\n";
> open (PASSWDfh, "<$PASSWD") || die "Can not open the file:
> $PASSWD. $!\n";
> open (XSHADOWfh, ">$XSHADOW") || die "Can not open the file:
> $XSHADOW. $!\n";
>
> @lines = <SHADOWfh> ;
> ### @linesX = <XSHADOWfh> # this is what I want
> foreach $line (@lines) {


Typically, you want to avoid reading the entire file into an array.
Instead, read the file, line by line, storing what's needed from
those lines.

e.g.
while( my $line = <SHADOWfh> )

or

open( my $shadow, '<', $SHADOW ) or die "...";
while( my $line = $shadow )

> ## what goes here?
> # Skip comments
> next if (m/^#/);
> # Skip blank lines
> next if (m/^\s*$/);
> ($username,$pass,$lastchange) = split /:/, $line;


my( $username,$pass,$lastchange) = (split /:/, $line)[0,1,2];

> ## what goes here?
> $x_lastupdate = ($lastchange*86400);

Who knows, you didn't mention anything about this column.
> #
> print $XSHADOWfh ("username: %s password: %s lastupdate: %d
> \n", \
> $username,$pass,$x_lastupdate) ;
> } # for each
> close $SHADOWfh;
> close $XSHADOWfh;
> close $PASSWDfh;
> @args = ("pwdck", "-y", "ALL");
> system(@args) == 0 or warn "System @args failed: $?";
> }
>
> Thank you very much,
>

 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      06-05-2007
J. Gleixner wrote:
> ) = split /:/, $line;Vahid wrote:

Sorry.. about that. bad paste on my part.

>>Vahid wrote
>> Hi all,

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging two DLL files n o s p a m p l e a s e C Programming 10 06-19-2007 08:57 PM
Merging Two Files using C++ ckoniecny@gmail.com C++ 5 09-24-2006 07:37 PM
?Merging files based on first two comma delimited fields? parkerdg@gmail.com Perl Misc 8 09-17-2005 08:47 PM
merging two XML files Stefan Franke XML 5 01-04-2005 09:23 PM
Merging two text files based on some kind of text anchors triangle Perl Misc 1 01-30-2004 09:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57