) = 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,
>
|