in reply to Insert and attach sequence of words in txt file if it exist in other file

Try reading the b file first and then match the words to those in a

#!/usr/bin/perl use strict; use Data::Dumper; my %file2 = (); open FILE2,'<',"./b.txt" or die "Cannot open b.txt"; while (<FILE2>){ my @words = split /\s+/,$_; my $zz = join 'zz',@words; for (@words){ $file2{$_} = $zz; } } close FILE2; print Dumper \%file2; open FILE1,'<',"./a.txt" or die "Cannot open a.txt"; open FILE3,'>',"./r.txt" or die "Cannot create r.txt"; while (<FILE1>){ chomp; my @words = split /[ \.]/,$_; my @added; for (@words){ push @added,$file2{$_} if exists $file2{$_}; }; print FILE3 $_,(join ' ',@added),"\n"; } close FILE1; close FILE3;
poj
  • Comment on Re: Insert and attach sequence of words in txt file if it exist in other file
  • Download Code

Replies are listed 'Best First'.
Re^2: Insert and attach sequence of words in txt file if it exist in other file
by shoura (Novice) on May 13, 2017 at 20:35 UTC
    this work thanks but here it copie sequence of words after . but me i want that it insert it before . i have some ligne in the a.txt that is like this <title> (here text)...</title > so in this case the sequence should be copied before </title>. should i change my @words = split / \./,$_; ?

      Is the . always at the end of the line ?

        yes