Modify a text file directly

S

Steve Hémond

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
 
M

Martien Verbruggen

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 :

See the Perl FAQ, Section 5, question

"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
 
B

Bob Walton

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).


....
 
J

John W. Krahn

Steve said:
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");
^ ^
This has been discussed to death in another thread but you don't need
quotes there. Either open(INPUT,$ARGV[0]) or open(INPUT,"<$ARGV[0]") is
what you want.

$in = <INPUT>;
close INPUT;

Then I do my modifications to the file and I (must) write the replacements
into another file :

No, you don't have to.

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?

There is a FAQ about file locking that describes how to do what you
want.

perldoc -q "I just want to increment the number in the file"



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top