Hi!
Here is what I want to know:
I have a data (text) file with many lines. I want to change something in line let's say 100 but leave all the rest intact.
Is there a quicker way to do this instead of:
#########
$myline = 100; # the line I want to edit
#### Get data
open(DATA, "data.txt"
;
@data = <DATA>;
close(DATA);
$line = $#data + 1;
#### Now re-writing the file
open(DATA, ">data.txt"
;
flock(DATA,2);
foreach $data_line (@data) {
chomp $data_line;
if($line == $myline) {print DATA "this is my change\n";}
else {print DATA "$data_line\n";}
}
close(DATA);
#############
If not, how can I make sure that no script writes to the data file from the time I read the data from data.txt to the time I start re-writing the file (and use flock)?
Thanks in advance!
Here is what I want to know:
I have a data (text) file with many lines. I want to change something in line let's say 100 but leave all the rest intact.
Is there a quicker way to do this instead of:
#########
$myline = 100; # the line I want to edit
#### Get data
open(DATA, "data.txt"
@data = <DATA>;
close(DATA);
$line = $#data + 1;
#### Now re-writing the file
open(DATA, ">data.txt"
flock(DATA,2);
foreach $data_line (@data) {
chomp $data_line;
if($line == $myline) {print DATA "this is my change\n";}
else {print DATA "$data_line\n";}
}
close(DATA);
#############
If not, how can I make sure that no script writes to the data file from the time I read the data from data.txt to the time I start re-writing the file (and use flock)?
Thanks in advance!