Hi, Christophe
I suggest the following (see attached file: modcfg)
Hope this helps.


folschette a écrit :
> 
> hi again,
> and which i forgot to say: while merging file1 into file2 , no key should be
> double, the right key is the one in file1!!
> 
> so in fact it is a replacement of some lines in file2 by some lines in
> file1, and the lines that don't exist yet in file2 are appended.
> 
> thanx, christophe folschette
> 
> Folschette wrote:
> 
> > hi again,
> >
> > i've got some problems using your script:
> >
> > best is if i give you the three files so here they are
> > file1 should  be merged in file2 but file2 should have the same layout as
> > befor merging
> >
> > christophe folschette
> >
> >
> > Rob wrote:
> >
> >> Christophe
> >>
> >> I think using Tie::File is overkill here. Try this:
> >>
> >> #   Merge the two files into a single hash
> >> #
> >>     for $file ( 'file2.dat', 'file1.dat' )
> >>     {
> >>         open FILE, "< $file";
> >>
> >>         while ( <FILE> )
> >>         {
> >>             chomp;
> >>             ($key, $val) = split /:\s+/;
> >>             $data{$key} = $val;
> >>         }
> >>
> >>         close FILE;
> >>     }
> >>
> >> #   and splat it out again
> >> #
> >>     open FILE, "> file3.dat";
> >>     printf FILE "%s: %s\n", $_, $data{$_}
> >>             for (sort keys %data);
> >>     close FILE;
> >>
> >> I'm not sure about your 'some text'. If you're allowing comment lines
> >> starting with a hash then
> >>
> >>     next if /^#/;
> >>
> >> at the start of the inner loop will do. Now if you want the comments
> >> retaining, that's another matter :))
> >>
> >> I never like posting just a solution on the beginners' group, but I don't
> >> think I'm doing anything obscure here that needs explaining. Tell me if
> >> I'm wrong.
> >>
> >> HTH.
> >>
> >> Cheers,
> >>
> >> Rob
> >>
> >> ----- Original Message -----
> >> From: "folschette" <[EMAIL PROTECTED]>
> >> To: <[EMAIL PROTECTED]>
> >> Sent: Tuesday, October 15, 2002 10:56 AM
> >> Subject: file to file copy
> >>
> >>
> >>> hello,
> >>> i have to write a perl script which copies text from one file to another
> >> but
> >>> only if the text is not exisiting yet.
> >>> For example:
> >>> in file1:
> >>> word: moon
> >>> word2: sky
> >>> ...
> >>> the same syntax for every line
> >>>
> >>> in file2:
> >>> #some text
> >>> word: honey
> >>> word3: lol
> >>> word4: mu
> >>> ...
> >>> as well the same syntax for every line
> >>>
> >>> so now i want to merge file1 into file2, so that word: honey will be
> >>> replaced by word: moon and word2: sky will be appended to file2.
> >>> i have written the following script but i've got little problem with it,
> >> can
> >>> someone help me? or test it?
> >>>
> >>> thanx,  christophe folschette
> >>
> >>
> >>
> >
> ----------------------------------------------------------------------------
> >> ----
> >>
> >>
> >>> --
> >>> To unsubscribe, e-mail: [EMAIL PROTECTED]
> >>> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
#!/usr/local/bin/perl -w
use strict;
my %modif;
my ($key, $val);
# first  store modifs from file1 to a hash
# assuming the format is:  keyword:<whitespace>value
# keywords and values are only one word each !
open(F1, "./file1.txt") or die "Can't open file1.txt for reading.\n";
while(<F1>) {
  chomp;
  ($key, $val) = split();
  $modif{$key} = $val;
}
close(F1);
# now copy file2 to file3, modifying lines as needed
open(F2, "./file2.txt")  or die "Can't open file2.txt for reading.\n";
open(F3, ">./file3.txt") or die "Can't open file3.txt for writing.\n";
while(<F2>) {
  chomp;
# assume 'interesting' lines always contain some_word: some_text
# if that key already exists in the hash (in file1)
# - prepare line in $_ for further printing in F3
# - delete that (precessed) key in the hash
# always print the line in F3 (modified or unchanged)
  if(/:/) {
    ($key, $val) = split();
    if (defined($modif{$key})) {
      $_ = "$key $modif{$key}";
      delete $modif{$key};
    }
  }
  print F3 "$_\n";;
}
# last, print a line to file3 for each key remaining in the hash
while (($key, $val) = each(%modif)) {
  print F3 "$key $val\r\n\n";
}
close(F2);
close(F3);
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to