Perl code to substituting one line in file

C

cindyybl

I am trying to edit a file in my perl program. Basically, the operation
is to substitute one line in that file. But I thought that file
read/write is too complicated and tedious for a simple task like this.

There is a one-liner to substitute text in selected file on command
line. And this is
perl -pi -e "s/pattern1/pattern2/g" filename

Well, I can always use back tick to exec this line in my program, but
is there a simple piece of perl code that will just do the same as that
in a perl program?
Thanks.
 
J

J. Gleixner

I am trying to edit a file in my perl program. Basically, the operation
is to substitute one line in that file. But I thought that file
read/write is too complicated and tedious for a simple task like this.

Ahhhh.. without file read/write how would you modify the contents of a file?
There is a one-liner to substitute text in selected file on command
line. And this is
perl -pi -e "s/pattern1/pattern2/g" filename

Well, I can always use back tick to exec this line in my program, but
is there a simple piece of perl code that will just do the same as that
in a perl program?

That is a perl program.

You won't get any shorter than that. This should answer all your questions.

perldoc -f open

It'd be 2 open()'s, a while loop, the substitution on each line, and
writing each line out to a file.

Also, perldoc perlrun, look for 'overwrite' and you'll see something
such as:

"From the shell, saying

$ perl -p -i.orig -e "s/foo/bar/; ... "

is the same as using the program: ..."

Post what you have tried.

If you want something short and sweet, check out the IO::All module,
however by the time you download, install, and read the documentation,
you could have written a short script to do it.
 
J

jl_post

I am trying to edit a file in my perl program. Basically, the operation
is to substitute one line in that file. But I thought that file
read/write is too complicated and tedious for a simple task like this.

There is a one-liner to substitute text in selected file on command
line. And this is
perl -pi -e "s/pattern1/pattern2/g" filename

Try this:

# Replace "pattern1" with "pattern2" in file "filename":
{local($^I,@ARGV)=("","filename"); s/pattern1/pattern2/g,print
while <>}

Note that the "I" in "$^I" is a capital "I" (eye) and not a lower-case
"l" (ell).

This line of code (technically more than one, but who's counting?)
sets the inplace-edit extension (which is mentioned in "perldoc
perlvar"). Since the above line of code sets it to something other
than undef, in-place editing will happen on all files specified in what
we just set @ARGV to be (in this case, ("filename")).

Also note the use of local(). We used it so that the previous
values of $^I and @ARGV would be restored at the end of the block.
Well, I can always use back tick to exec this line in my program, but
is there a simple piece of perl code that will just do the same as that
in a perl program?

Don't use backticks here. Use a call to system() instead. To
understand why, read "perldoc -q backticks".

I hope this helps, Cindy.

-- Jean-Luc
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top