PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:

So here is what I have: This just appends the end of the file which is close but not what I need. I need to add fileA in the MIDDLE of FileB based on the search term "XYZ" There is a marker in the middle of the file which does not change.
#!/usr/bun/perl #------------------------------ # This script searches for "search" # and adds lines from fileSource to fileOutput use warnings; use strict; #use Fcntl (:flock :seek); # Check unless(-e $ARGV[0] || -e $ARGV[1]){ print("Please Enter A Valid File Name\n"); exit 1; } #if($ARGV[0] == "--help"){ # print("Usage is:\n"); # print("perl conference_add.pl <Source File> <Output File> <Sear +ch String>"); #} # assigns file path to local variables my($fileSource) = $ARGV[0]; my($fileOutput) = $ARGV[1]; my(@inputLines); # opens files for reading/writing open(FILESOURCE, "<", $fileSource) || die("Can not open $fileSource $! +"); open(FILEOUT, ">>", $fileOutput) || die ("Can not open $fileOutput $!" +); @inputLines = <FILESOURCE>; print FILEOUT @inputLines; close(FILESOURCE); close(FILEOUT);

fileA:
1 2 3 4 5

fileB
a b c d SEARCHPATTERN e f g h i

end result:
a b c d SEARCHPATTERN 1 2 3 4 5 e f g h i
please help.

Replies are listed 'Best First'.
Re: Modifying File in the Middle
by JavaFan (Canon) on Apr 10, 2010 at 19:43 UTC
    1. Open fileC for writing.
    2. Open fileB for reading.
    3. Read in fileB up to and including the SEARCHPATTERN.
    4. Write everything you've read into fileC.
    5. Open fileA for reading.
    6. Read in fileA, write it to fileC.
    7. Close fileA.
    8. Read the rest of fileB, writing it to fileC.
    9. Close fileB.
    10. Close fileC.
    11. Rename fileC to fileB.
    Or in an (untested) Perl/shell one-liner:
    perl -ne 'print; print `cat fileA` if /SEARCHPATTERN/' fileB > fileC & +& mv fileC fileB
Re: Modifying File in the Middle
by tospo (Hermit) on Apr 11, 2010 at 16:40 UTC

    You can do it like this:

    #!/usr/bin/perl use strict; use warnings; my $usage ="$0 <fileA> <fileB>\n"; my $fileA = shift or die $usage ; my $fileB = shift or die $usage; open(FILEA, '<', $fileA) or die "could not open $fileA\n"; open(FILEB, '<', $fileB) or die "could not open $fileB\n"; my $file_a_content; { local $/=undef; # unset input record separator to slurp all into a s +tring $file_a_content = <FILEA> ; close FILEA; } while (my $line = <FILEB> ) { $line=~s/SEARCHPATTERN/$file_a_content/; print $line; } close FILEB;

    This will print to STDOUT, so use it like this:

    $ middle_replacer.pl fileA fileB > fileC
Re: Modifying File in the Middle
by kiruthika.bkite (Scribe) on Apr 12, 2010 at 04:00 UTC
    Hi,
    Try with the following.
    use Tie::File; use Fcntl 'O_RDONLY','O_RDWR'; my @file1; my @file2; my $file1; tie @file1, 'Tie::File', "fileA",mode => O_RDONLY; tie @file2, 'Tie::File', "fileB",mode => O_RDWR; $file1=join("\n",@file1); for($i=0;$i<=$#file2;$i++) { next unless($file2[$i]=~s/(SEARCHPATTERN)/$1\n$file1/) }
    fileA<br> 1 2 3 4 5 fileB a b c d SEARCHPATTERN e f g h i Ouput(fileB) a b c d SEARCHPATTERN 1 2 3 4 5 e f g h i