Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing a line in a data file

Status
Not open for further replies.

c4n

Programmer
Mar 12, 2002
110
0
0
SI
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, &quot;>data.txt&quot;);
flock(DATA,2);

foreach $data_line (@data) {
chomp $data_line;
if($line == $myline) {print DATA &quot;this is my change\n&quot;;}
else {print DATA &quot;$data_line\n&quot;;}
}

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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top