Hi all,
I have a file with some words starting with "RR_"... i want to change it to "TT_"...
For example.... a word in the file "test.txt" ... is "RR_Word"...i want it to be converted into "TT_Word"... There are 1000 such words in the file.
I am new to perl so please some one give me any idea??
|
Ambitious contributor
|
|
| 29Sep2008,20:34 | #2 |
|
Here it is with line-by-line processing, saving to a different output file:
Code:
open IN, "<test.txt"; # input file
open OUT, ">test2.txt"; # output file
while( <IN> ) { # read a line from IN into $_
s/\bRR_/TT_/g; # global substitute on $_
# (\b means word boundary)
print OUT $_; # write $_ to OUT
}
close IN; # close files
close OUT;
Code:
undef $/; # enable file-slurping mode open IN, "<test.txt"; $str = <IN>; # slurp in whole file close IN; $str =~ s/\bRR_/TT_/g; # substitute open OUT, ">test.txt"; print OUT $str; # write whole file close OUT; |
|
Go4Expert Member
|
|
| 19Feb2010,12:47 | #3 |
|
Through command line itself we can achieve your requirement.
Code:
$perl -p -i.bak -e "s/RR_/TT_/g;" test.txt After executing the command you will have the both files "test.txt" and "test.txt.bak" [ RR_WORLD and TT_WORLD ]
|
