Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to change words between two file using perl ?

Status
Not open for further replies.

mingchung

Technical User
Joined
Nov 24, 2006
Messages
4
Location
TW
Hi~~
If i have two files (aaa.txt & bbb.txt):

aaa.txt:
first_data = 22.55e-3
second_data = 99552
.........


bbb.txt:
......(first_data)......(second_data).......


i want to use perl to find first_data & second_data in aaa.txt
then replace in bbb.txt
How should i do ?
thanks~~!!

i am a analog designer
this is the first time use perl

 
regular expressions are what you want
Code:
open FH, "<aaa.txt" or die $!;
while (<FH>) {
   chomp ($_); #remove newlines
   ($setting, $value)=split (/=/, $_);
   $settings{$setting}=$value;
}
close FH;
open FH, "<bbb.txt" or die $!;
$/=undef; #slurp file
$text=<FH>;
close FH;
foreach $key (keys $settings) {
  [COLOR=red]$text=~s/$key/$settings/mg; [/color]#g replace globally, m allow mutli line matches
}
open OP, ">bbb.txt";
print OP $text;
close OP;

Not tested, but should help you along the way ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
PaulTEG:
thanks for your reply~~
i use your code
but has some error
the error message is :

Type of arg 1 to keys must be hash (not scalar dereference) at change_word.pl line 12, near "$settings) "
Execution of change_word.pl aborted due to compilation errors.

what should i do?
thanks~~~!!!!
 
and if i change line 12 to
foreach $key (keys %settings) {


then the bbb.tat will :
......( )......( ).......


there is no data in......
thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top