merging two files

V

Vahid

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,
 
J

J. Gleixner

) = split /:/ said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top