Ask a Question related to PERL Miscellaneous, Design and Development.
-
Steve Hémond #1
Modify a text file directly
Hi all,
I have to replace some chuncks out of a text file. My actual way to do this
is to put the file contents into a scalar variable :
open (INPUT, "$ARGV[0]") or die ("Can't open file : $!\n");
$in = <INPUT>;
close INPUT;
Then I do my modifications to the file and I (must) write the replacements
into another file :
open (OUTPUT, ">$ARGV[1]") or die ("Can't create file : $!\n");
seek (OUTPUT,0,0);
print (OUTPUT $in);
close OUTPUT;
That way, I must save the replacements to ANOTHER file, which I don't want.
I would like to modify the file DIRECTLY. Since I cannot modify the file
using the file handle like this :
<INPUT> = s/blahblah/blah2blah2/g
How could I modify the file directly?
Thanks in advance,
Steve
Steve Hémond Guest
-
Is there an easy way to just modify text content with VB
Hello, I just want to change any given text in a PDF document programatically. For instance, changing the language of the text within a PDF document... -
Adding text directly into keyframes, creating tweens
Hello, I would like to create a Word Document and import that word document into Flash. I'm not talking about data integration from an outside... -
How to *modify* text in HTML::Element
Sorry for the repost, I hit Send too soon. I am using HTML::TreeBuilder to futz with some HTML, and I need to modify the text of some of the... -
text modify
Hi, I have some data column based data I want to modify a bit. 0065 663 517 046 0 1485 0065 663 517 046 3 1500 0065 663 517 046 5 1882... -
Modify Text in a Combo Box A2002
I'm trying to validate and change (if necessary) what a user types in a combo box, specifically a person's name. The bound combo box presents... -
Martien Verbruggen #2
Re: Modify a text file directly
On Thu, 18 Sep 2003 16:58:23 -0400,
Steve Hémond <shemond@hotmail.com> wrote:See the Perl FAQ, Section 5, question> Hi all,
>
> I have to replace some chuncks out of a text file. My actual way to do this
> is to put the file contents into a scalar variable :
"How do I change one line in a file/delete a line in a file/insert a
line in the middle of a file/append to the beginning of a file?"
Martien
--
|
Martien Verbruggen | This matter is best disposed of from a great
Trading Post Australia | height, over water.
|
Martien Verbruggen Guest
-
Bob Walton #3
Re: Modify a text file directly
Steve Hémond wrote:
....
> I have to replace some chuncks out of a text file. My actual way to do this
> is to put the file contents into a scalar variable :
>
> open (INPUT, "$ARGV[0]") or die ("Can't open file : $!\n");
> $in = <INPUT>;
> close INPUT;
>
> Then I do my modifications to the file and I (must) write the replacements
> into another file :
>
> open (OUTPUT, ">$ARGV[1]") or die ("Can't create file : $!\n");
> seek (OUTPUT,0,0);
> print (OUTPUT $in);
> close OUTPUT;
>
> That way, I must save the replacements to ANOTHER file, which I don't want.
> I would like to modify the file DIRECTLY. Since I cannot modify the file
> using the file handle like this :
>
> <INPUT> = s/blahblah/blah2blah2/g
>
> How could I modify the file directly?
Couple of things you should check out:
1. The -i switch on the perl command. See perldoc perlrun for details.
Also check out the -p and -n switches while you're at it. Something
on the order of
perl -i.bak -pe "s/blahblah/blah2blah2/g" filename.ext
might suffice. Of course, behind the scenes, something similar to what
you wrote is happening (although the file isn't being slurped) -- this
is just a very convenient shorthand for folks with the virtue of laziness.
2. The Tie::File module will let you tie a file to an array, with one
line per array element. You may modify the array elements, which will
cause the file to be modified. Again, behind the scenes, this is still
actually doing something somewhat similar to what you were originally
doing -- there is no other way to accomplish it, unless you happen to be
replacing stuff character-for-character (in which case check out seek
and tell).
....
--> Steve
Bob Walton
Bob Walton Guest



Reply With Quote

