Ah, now that's a different kettle of fish. (English fish kettles are a bit like American ball parks. Je ne sais pas la métaphore Française. ;)
File1 is a list of key/value pairs which should be used to modify File2 such that 1/ Every line in File2 which has a key matching one of those in File1 takes its value from File1 2/ Any line in File1 with a key not previously mentioned in File2 should be appended to File2 3/ All other lines in File2 should remain in sequence and unchanged Is that right? Your example has what must be continued lines, terminated by a backslash. It's not too bad as long as we can assume the following: 1/ We can ignore anything in File1 that insn't a key/value pair (no comments or blank lines) 2/ We don't have to support continued lines for File1 3/ We don't need to preserve the number of spaces between the colon and the value field 4/ We don't have to expand stuff like ${start_url} My first crack at this gives: =perl # Pull all the key/value pairs from file1 into a hash # whose values are array references [ value string, usage count ] # open FILE, "< file1.txt"; while ( <FILE> ) { next if /^#/ or /^\s*$/; # Ignore blank lines and comments die "Can't accept continued lines" if /\\$/; chomp; ($key, $val) = /([^:]+):\s*(.+?)\s*$/; $merge{$key} = [$val, 0]; } close FILE; # Open the merged output file # open MERGE, "> file3.txt"; # Translate all the lines in file2 to the output file # according to the revisions in %merge # open FILE, "< file2.txt"; while ( <FILE> ) { chomp; if ( /^[^\s#].*:/ ) { ($key) = /^([^:]+)/; if ( exists $merge{$key} ) { $_ = "$key: $merge{$key}[0]"; ++$merge{$key}[1]; } } print MERGE "$_\n"; } close FILE; # Append all the key/value pairs in the hash that still # have a zero usage count # for $key ( keys %merge ) { next if $merge{$key}[1]; print MERGE "$key: $merge{$key}[0]\n"; } close MERGE =cut Which works with your given data. Finishing the comments is left as an exercise for the reader. The PerlFAST and PerlBRIEF speakers in the group can refine it and demonstrate TMTOWTDI once more. Cheers all, and bonne chance R ----- Original Message ----- From: "folschette" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, October 16, 2002 9:24 AM Subject: Re: file to file copy > 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] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]