cancel
Showing results for 
Search instead for 
Did you mean: 

Update a word in a line

pgp_acc1
Advisor

Update a word in a line

Hi,

I'm trying to update a line in the backup file,but its not updating properly.

use File::Copy; @files=;
foreach $event (@files) {
my $backup=$event.".bak";
copy($event,$backup);
open(MYFILE,"<$backup")|| die "cant find it";;
while () {
#print $_."\n";
if (/^not/) {
s/OCAL/OWN/e;
print $_."\n";
}
}
close(MYFILE);
}
15 REPLIES
OldSchool
Honored Contributor

Re: Update a word in a line

gee....
"but its not updating properly."

so, is it erroring out? changing something you don't want it to? changing nothing?

do you suppose posting the contents (or a relevant portion thereof) of the original file might help? Along with a (detailed) description of the desired change?

James R. Ferguson
Acclaimed Contributor

Re: Update a word in a line

Hi:

You are apparently trying to do updates while creating backup copies of files. There is an easier way.

First, your code doesn't update anything since you don't write to anything other than STDOUT. There are other errors too.

You could do this:

# cat ./myupdater
#!/usr/bin/perl
use strict;
use warnings;
{
local $^I = '.bak';
local @ARGV = ( );
while (<>) {
if (/^not/) {
s/LOCAL/OWN/g;
}
print;
}
}
1;

Regards!

...JRF...
pgp_acc1
Advisor

Re: Update a word in a line

Hi,

Sorry!!
File format like below:

file:dsjfsdkaf
header:iewrweur
not:updat,OCAL,3247283,wrwe
not:updat,OCCL,32424323,werwe
footer:werwer

The OCAL i want to change to OWN.
And the file is copied as it is ,the change is not done as required.

But this oneliner works :
perl -p -i.bak -e 'if (/^not/) { s/OCAL/OWN/; } ' *05

Thanks,
Priya.
James R. Ferguson
Acclaimed Contributor

Re: Update a word in a line

Hi (again):

Two more comments. The code I offered above is the equivalent of a commandline edit and could be reduced to that too.

Second, please don't forget to evaluate the answers to your previous question(s):

http://forums13.itrc.hp.com/service/forums/questionanswer.do?threadId=1347038

Regards!

...JRF...
Durvesh Mendhekar
Regular Advisor

Re: Update a word in a line

Hi,

Use fbackup instead of tar as fbackup have more features.

http://forums13.itrc.hp.com/service/forums/questionanswer.do?threadId=663262&admit=109447627+1245161973650+28353475

Regards,
Durvesh
Durvesh Mendhekar
Regular Advisor

Re: Update a word in a line

Hi,

Sorry...........

Durvesh
pgp_acc1
Advisor

Re: Update a word in a line

James,
I tried the same code as given but its still not updating the file for OCAL to OWN.
The file is just copied.

Thanks,
Priya
James R. Ferguson
Acclaimed Contributor

Re: Update a word in a line

Hi (again):

> Still don't know why I can login to some non-root users via telnet, but not others.

Well, I thought you meant change LOCAL to OWN. My code was written to affect that change, so amend it the way you need it.

Regards!

...JRF...
pgp_acc1
Advisor

Re: Update a word in a line

It worked now thanks :)
Can you tell me what was wrong in the first code?
And secondly the number of files will be huge,maybe like 3-4lakhs.So will @ARGV have any issue?

Priya
James R. Ferguson
Acclaimed Contributor

Re: Update a word in a line

Hi (again):

> Can you tell me what was wrong in the first code?

As I said, you never wrote to anything other than STDOUT. Remember you can 'select()' a default filehandle or explicitly use one but you did neither. The '/e' modifier in your regex whould have been '/g'. You were attempting to evaluate and execute while substituting and there was nothing legal in what you wrote.

> And secondly the number of files will be huge,maybe like 3-4lakhs.So will @ARGV have any issue?

No, this should not be an issue.

By the way, what does "lakhs" stand for? You have used that term before.

Regards!

...JRF...
pgp_acc1
Advisor

Re: Update a word in a line

I still didn't get it. :(

1 lakh = 100,000
Dennis Handly
Acclaimed Contributor

Re: Update a word in a line

>JRF: what does "lakhs" stand for?

You didn't remember when I asked that question? :-)

Basically the OP should stick with SI units or actual numbers or scientific notation.
pgp_acc1
Advisor

Re: Update a word in a line

James,
It worked after these changes, can you tell me if these are correct?

foreach $event (@files) {
my $backup=$event.".bak";
open(MYFILE,"<$backup")|| die "cant find it";;
open(MYFILE1,"<$event")|| die "cant find it";;

while () {
if (/^not/) {
($line=$_) =~ s/OCAL/OWN/e;
print MYFILE $line;
}
else { print MYFILE $_; }
}
close(MYFILE);
}
Patrol
Advisor

Re: Update a word in a line

>JRF: what does "lakhs" stand for?

>Dennis: Basically the OP should stick with SI units or actual numbers or scientific notation.

Funny, really funny.I think neither of you have read the memo, perttaining to renaming the ITRC.

...
From now on, the website formerly known as Information Technology Response Center, a.k.a., ITRC, will be renamed as

Indian Tech Rescue Center

We are hoping that, keeping the same abbreviation, will help you adapt the change faster and better.
...


A simple wikipedia search would show you the meaning of word lakh being a hundred thousand. If you have noticed the majority of the names floating around ITRC nowadays, it is expected by the original poster of the question that everyone should understand the Indian terminology, despite how prudish t expectation sounds.

Viva globalism !
James R. Ferguson
Acclaimed Contributor

Re: Update a word in a line

Hi (again):

> It worked after these changes, can you tell me if these are correct?

Yes, I know it worked --- I tested it too.

Your suggestion will _not_ work. You didn't read what I said about the '\e' modifier. You open both files for input.

If you would get in the habit of using the 'strict' and 'warnings' pragmas you will catch many stupid errors the first time you attempt to run your script:

use strict;
use warnings;

Add these and make the necessary changes.

By the way, thanks Dennis & Patrol. I should have known Dennis that you would have asked that too :-) --- You and I do think alike.

Regards!

...JRF...