replace multiple lines in a file with Perl

L

learner

Hi, all,
I 've searched high and low, just couldn't find a single decent
example to replace multiple lines from a file.

So I did this:

#!/usr/local/bin/perl
undef $/;
$original = "
// This is an old text
// that is across several
// lines in this file. This file
// also contains the copy right
// symbol \251.";
$replacement = "
// I just want to replace
// the old lines in one fell
// swoop!";
$sHOME = "./changed/";
foreach $file (@ARGV) {
if (! open(INPUT,"<$file") ) {
print "Can't open input file $file\n";
next;
}
$data=<INPUT>;
close INPUT;

if ($data =~ s/$original/$replacement/g) {
$newfile = $sHOME.$file;
print $newfile."\n";
if (! open(OUTPUT,">$newfile") ) {
die "Can't open output file $newfile\n";
}
print OUTPUT $data;
close OUTPUT;
print "$file changed\n";
}
else { print "$file not changed\n"; }
}
exit(0);

Then I would do:
perl thisperlfile.pl name.of.file.with.original.txt

The input file contains only one copy of the original text, but it
created more than 50 replacement txt in the final file. I am really
puzzled. Could someone help please? Thanks.
 
R

Robert Sedlacek

learner said:
s/$original/$replacement/g

Sorry, too early to read it completely, but you may find help in the docs
for regular expressions. Look out for the modifiers (here you just have g).
Maybe you should also look at qr//.

g,
Robert
 
A

Arndt Jonasson

learner said:
I 've searched high and low, just couldn't find a single decent
example to replace multiple lines from a file.

[...]

The input file contains only one copy of the original text, but it
created more than 50 replacement txt in the final file. I am really
puzzled. Could someone help please? Thanks.

I ran your program, but it seemed to do what it should with my simple
input file. Exactly what does your input look like?
 
L

learner

Well, did you read my first post, I did include a 'short and complete
program *that you can run*'.
 
L

learner

OK, here is what I actually need to do. we have this big program
project that has many source code files, and I want to replace the old
copyright notice that is included in many files with a new one.

The old one looks something like this:

// MyBigBuckSoft
//
// Laboratory for Applications of Land Surveying
// XXXYYY PPPPPP
// West xxxxyyyyy, XX 55555
// Copyright (1988-2004)
// © Xxxyyy RKKKKK Foundation
// All rights reserved.

Then I will need to replace it with a new one across multiline. If you
could, please try to replace this old txt to see if you get the same
kind of weird result as I did. I am not sure if that 'copyright' symbol
is the reason?!
 
P

Paul Lalli

learner said:
Well, did you read my first post, I did include a 'short and complete
program *that you can run*'.

Oh, okay, let me go run that file....

Can't open output file name.of.file.with.original.txt

Gee. Doesn't work.

Posting a file that can be run, when dependent on external data,
requires the use of the __DATA__ marker. We don't have any sample input
data with which to run your program.

Please (re-)read the Posting guidelines, paying attention to the section
labeled "Provide enough information"

Paul Lalli
 
L

learner

Ok, Please do these steps to run the program I posted:
1. copy and paste the perl program into an .pl file, say it is 'rep.pl'
2. in the same directory, create a new directory called 'changed'
3. create a new file with whatever name you want, say it is called
'oldtxt', which contains the oldtxt that you like to replace (the same
txt listed in the program)
4. then you issue command :

perl rep.pl oldtxt
to run it.
Hope this will help, and see if you can help me out in return. Thanks.
 
G

Gunnar Hjalmarsson

learner said:
OK, here is what I actually need to do. we have this big program
project that has many source code files, and I want to replace the old
copyright notice that is included in many files with a new one.

The old one looks something like this:

// MyBigBuckSoft
//
// Laboratory for Applications of Land Surveying
// XXXYYY PPPPPP
// West xxxxyyyyy, XX 55555
// Copyright (1988-2004)
// © Xxxyyy RKKKKK Foundation
// All rights reserved.

Then I will need to replace it with a new one across multiline. If you
could, please try to replace this old txt to see if you get the same
kind of weird result as I did. I am not sure if that 'copyright' symbol
is the reason?!

No, the copyright character is not special in regular expression
patterns, but parentheses are, so they need to be escaped. You can do
this when assigning the $original variable:

Copyright \\(1988-2004\\)

but the easiest way to deal with it is to use \Q:

s/\Q$original/$replacement/g

Please see "perldoc perlre".

HTH
 
B

Brian McCauley

learner said:
Ok, Please do these steps to run the program I posted:
1. copy and paste the perl program into an .pl file, say it is 'rep.pl'
2. in the same directory, create a new directory called 'changed'
3. create a new file with whatever name you want, say it is called
'oldtxt', which contains the oldtxt that you like to replace (the same
txt listed in the program)
4. then you issue command :

There's only one of you, there are many people who may want to see if
they help you. As such it is more efficient for you (one person) do
take the steps necessary to produce a mimimal but complete
(self-contained) test case rather than for everyone else to have to take
these steps.
 
P

Paul Lalli

Ok, Please do these steps to run the program I posted:

Explain to me why you believe I (or any other potential reader of this
group) should have to follow *your* instructions? YOU are the one
asking for help. WE are the ones generously donating our time and
effort for the chance of helping YOU. That means you do things the way
WE ask you to. And *that* means following the Posting Guidelines.

Paul Lalli
 
T

Tad McClellan

learner said:


"OK" implies agreement, yet you are not agreeing to post a short
and complete program that we can run...

Please do these steps to run the program I posted:


Send me a check *before* you start issuing instructions to me.

I'm busy.

see if you can help me out in return.
^^^^^^^^^

In return for *what*?

You haven't done anything for me, you haven't even done what
I asked of you.


If you make it easy for people to answer your question you will
have a much better chance of getting an answer to your question.

If you don't, you won't.
 
A

Arndt Jonasson

learner said:
Ok, Please do these steps to run the program I posted:
1. copy and paste the perl program into an .pl file, say it is 'rep.pl'
2. in the same directory, create a new directory called 'changed'
3. create a new file with whatever name you want, say it is called
'oldtxt', which contains the oldtxt that you like to replace (the same
txt listed in the program)
4. then you issue command :

perl rep.pl oldtxt
to run it.
Hope this will help, and see if you can help me out in return. Thanks.

You were already told to please use the __DATA__ marker to exhibit
a complete program that fails, including data. It's good advice.
The comments you're getting are not because it's so very hard for anyone
to fill in missing details in your problem description, but because it
should be you doing that work, and besides, those who tried that approach
couldn't reproduce the problem.
 
L

learner

Well, everyone,
I thought the little program I posted is such an simple endeavor
that it would be really obvious for gurus to disect... and then the
"instruction" i gave, which invoke so many ANGRY people. Appologize to
you all!
Peace.
 
A

A. Sinan Unur

Well, everyone,
I thought the little program I posted is such an simple endeavor
that it would be really obvious for gurus to disect... and then the
"instruction" i gave, which invoke so many ANGRY people.

Because your instructions presumed that the two minutes it would have taken
you to do it right was more valuable than the 30 or so minutes 15 different
people would have spent following your instructions. That is very rude.
Appologize to you all!

If the apology is sincere, then there might still be hope for you. You can
show that you are sincere by following the posting guidelines in your
future posts.

Sinan.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top