1753797 Members
7426 Online
108799 Solutions
New Discussion юеВ

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 15
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