Copy First file's content & Paste into second file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dillipkumar
    New Member
    • Mar 2008
    • 41

    Copy First file's content & Paste into second file

    Hi,

    I have 2 files having contents:

    target.txt
    ----------
    1=av
    2
    a=
    #abc
    4
    b=
    7
    8
    c=
    5=abc
    6=def
    d=
    8
    9


    source.txt
    ----------
    a=100
    c=200
    b=400
    d=300


    I want to copy all the data of source.txt file & paste into target.txt file. i.e. find all the data of target.txt in source.txt file, if found in source.txt file then copy the contents of source.txt file & paste into target.txt file in same location, like below output.


    target.txt
    ----------
    1=av
    2
    a=100
    #abc
    4
    b=400
    7
    8
    c=200
    5=abc
    6=def
    d=300
    8
    9


    I have written a perl script, but it is not working properly.

    Code:
    #! /usr/bin/perl -w
    
    
    open SRC, "source.txt" or die $!;
    while($src=<SRC>)
    {
      chomp($src);
      
      $src =~ s/^\s*//;  
      $src =~ s/\s*$//;
      
      ($f,$s)=split(/=/,$src);
      
      push(@spl_f_src, $f);
      push(@spl_s_src, $s);
    }
    
    open TGT, "+<target.txt" or die $!;
    while($tgt=<TGT>)
    {
    	chomp($tgt);
    	$tgt =~ s/^\s*//;  
    	$tgt =~ s/\s*$//;
    	push(@OC,$tgt);
    }
    
    for ($mv=0;$mv<=$#spl_f_src;$mv++)
    {
    	for($ad=0;$ad <= $#OC;$ad++)
    	{	
    			if("$spl_f_src[$mv]=" eq $OC[$ad])
    			{
    				print "$spl_f_src[$mv]=$spl_s_src[$mv]\n";
    				mv++;
    				last;
    			}
    			else
    			{
    				print "$OC[$ad]\n";
    			}
    	$ad++:
    	}
    	
    }
    
    close(TGT);
    close(SRC);
    Please somebody help me to solve this.

    ~Thanks
    Last edited by numberwhun; Dec 5 '11, 07:56 AM. Reason: Please use code tags around your code in the forums!
Working...