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

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

Replies are listed 'Best First'.
Re^3: Copy data from one file and add it to another
by poj (Abbot) on Jan 24, 2018 at 11:06 UTC

    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.