473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl -w
  2.  
  3.  
  4. open SRC, "source.txt" or die $!;
  5. while($src=<SRC>)
  6. {
  7.   chomp($src);
  8.  
  9.   $src =~ s/^\s*//;  
  10.   $src =~ s/\s*$//;
  11.  
  12.   ($f,$s)=split(/=/,$src);
  13.  
  14.   push(@spl_f_src, $f);
  15.   push(@spl_s_src, $s);
  16. }
  17.  
  18. open TGT, "+<target.txt" or die $!;
  19. while($tgt=<TGT>)
  20. {
  21.     chomp($tgt);
  22.     $tgt =~ s/^\s*//;  
  23.     $tgt =~ s/\s*$//;
  24.     push(@OC,$tgt);
  25. }
  26.  
  27. for ($mv=0;$mv<=$#spl_f_src;$mv++)
  28. {
  29.     for($ad=0;$ad <= $#OC;$ad++)
  30.     {    
  31.             if("$spl_f_src[$mv]=" eq $OC[$ad])
  32.             {
  33.                 print "$spl_f_src[$mv]=$spl_s_src[$mv]\n";
  34.                 mv++;
  35.                 last;
  36.             }
  37.             else
  38.             {
  39.                 print "$OC[$ad]\n";
  40.             }
  41.     $ad++:
  42.     }
  43.  
  44. }
  45.  
  46. close(TGT);
  47. close(SRC);
  48.  
Please somebody help me to solve this.

~Thanks
Dec 5 '11 #1
0 1322

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
2
by: GMK | last post by:
Dear all in my asp.net application i have a text file that is installed with my application on the server. this text file is filled with data through a web interface in my application. i need to...
2
by: ray | last post by:
I want to reverse a large file's content to a new file by bytes. For example, If a file's content is 0A0B0C0D, I expected the result file's content is 0D0C0B0A. Is there is elegent and efficient...
5
by: Al | last post by:
Hi all We have created a xml file that imports a single project using the Import element. This project compiles to a class library, but has references to two other projects that are also class...
4
by: rony_16 | last post by:
Hi, I have a program that connects to a site With WebRequest and WebResponse . The response of this site is a file (csv file). The problem is that the file do not comes as a stream , hi is a part...
2
kamill
by: kamill | last post by:
i need to write content of one text file into another text file. My code is working ,if i choose both files from same directory where my program reside..BUT,its not working if i select files from...
2
by: deviparimala | last post by:
Hi All, I'm opening 2 notepads in my windows application. I copy data from one notepad and paste it another notepad. The data should not get pasted into the second notepad. Can I handle this...
0
by: arindams | last post by:
I've a form where an webrowser contains a html file. I want to copy a selective portion of the html content and paste that content in an excel 2007 file where it will be pasted as a normal text (not...
1
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From text file For example if the text file content is "Every man has a price. Every woman has a price." First Token(word) is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.