in reply to Copy data from one file and add it to another

#!/usr/bin/perl use warnings; use strict; my $infile = 'test.txt'; open INFO, '<', $infile or die "could not open '$infile' $!"; open FILEA, '<', 'outputfromvariantparser1.txt' or die "could not ope +n 'outputfromvariantparser1.txt' $!"; open FILEB, '>>', 'output.txt' or die "could not open 'output.txt' $!" +; my @raw_data = <FILEA>; while ( my $line = <INFO> ) { $line =~ s/TEXT TO BE REPLACED.*/$raw_data[0]/s; print FILEB $line; push @raw_data, shift @raw_data; }

Replies are listed 'Best First'.
Re^2: Copy data from one file and add it to another
by olus (Curate) on Apr 26, 2008 at 10:47 UTC

    Nice solution. Liked particularly the way you make the array go on circles. There is one thing though. Given that input data the regexp is not considering the |. Maybe $line =~ s/\| TEXT TO BE REPLACED.*/ $raw_data[0]/s;

Re^2: Copy data from one file and add it to another
by Anonymous Monk on Jan 24, 2018 at 09:27 UTC

    I have two files one is a Text file where I have some different Patterns and another one is ASCII file where I have code to generate a test pattern. I need Perl Script where I can transfer data pattern to that ASCII file, after particular word "PI".

    File 1-

    Result is : 10000000 10000000 10000000 10000000 10000 +000 00000011 00000011 00000011 00000011 00000011 10000001 10000001 10000001 10000001 10000001 11000000 11000000 11000000 11000000 11000000 01100000 01100000 01100000 01100000 01100000

    File 2-

    CHAIN_TEST = pattern = 0; apply "grp1_load" 0 = chain "chain1" = "010"; end; force "PI" "XX0XXXXXX" 1; measure "PO" "XX" 2; apply "grp1_unload" 3 = chain "chain1" = "010"; end; pattern = 1; apply "grp1_load" 0 = chain "chain1" = "101"; end; force "PI" "XX0XXXXXX" 1; measure "PO" "XX" 2; apply "grp1_unload" 3 = chain "chain1" = "101"; end; end; SCAN_TEST = pattern = 0; apply "grp1_load" 0 = chain "chain1" = "010"; end; force "PI" "100000000" 1; measure "PO" "10" 2; pulse "/CK" 3; apply "grp1_unload" 4 = chain "chain1" = "010"; end;

    2018-01-25 Athanasius added code and paragraph tags

      Put a unique marker in the file eg <PATTERN> where you want the substitution.

      #!/usr/bin/perl use strict; use warnings; my $infile1 = 'file1.txt'; my $infile2 = 'file2.txt'; my $outfile = 'result.txt'; #open IN,'<',$infile1 or die "$infile1 : $!"; #my @file1 = <IN>; #chomp(@file1); #close IN; my @file1 = qw(10000000 00000011 10000001 11000000 01100000 ); #open IN,'<',$infile2 or die "$infile2 : $!"; #my @file2 = <IN>; #close IN; my @file2 = <DATA>; open OUT,'>',$outfile or die "$outfile : $!"; for my $pattern (@file1){ print "Test pattern is $pattern\n"; for (@file2){ my $line = $_; # copy to preserve @file2 $line =~ s/<PATTERN>/$pattern/; print OUT $line; } } close OUT; __DATA__ SCAN_TEST = pattern = 0; apply "grp1_load" 0 = chain "chain1" = "010"; end; force "PI" "<PATTERN>" 1; measure "PO" "10" 2; pulse "/CK" 3; apply "grp1_unload" 4 = chain "chain1" = "010"; end;
      poj
        Thank you so much for your help.It's a big favor to me.