merging two files

Discussion in 'Perl Misc' started by Vahid, Jun 5, 2007.

  1. Vahid

    Vahid Guest

    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,
    Vahid, Jun 5, 2007
    #1
    1. Advertising

  2. Vahid

    J. Gleixner Guest

    ) = 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,
    >
    J. Gleixner, Jun 5, 2007
    #2
    1. Advertising

  3. Vahid

    J. Gleixner Guest

    J. Gleixner wrote:
    > ) = split /:/, $line;Vahid wrote:

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

    >>Vahid wrote
    >> Hi all,
    J. Gleixner, Jun 5, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Franke

    merging two XML files

    Stefan Franke, Jan 4, 2005, in forum: XML
    Replies:
    5
    Views:
    747
    Joris Gillis
    Jan 4, 2005
  2. Replies:
    5
    Views:
    518
    Jerry Coffin
    Sep 24, 2006
  3. n o s p a m p l e a s e

    Merging two DLL files

    n o s p a m p l e a s e, Jun 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,231
    CBFalconer
    Jun 19, 2007
  4. triangle
    Replies:
    1
    Views:
    102
    Gunnar Hjalmarsson
    Jan 30, 2004
  5. Replies:
    8
    Views:
    170
Loading...

Share This Page