Welcome, guest! Login
Close

Login to Your Account


  |  » Forgot your password?
Join

Notices

Thread: Perl: Editing a line in a data file

 
Tools Search
  #1  
Old August 2nd, 2002, 12:33 PM
c4 is offline
c4's Avatar
Full Member
Join Date: January 17th, 2005
Posts: 488
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!
  #2  
Old August 21st, 2002, 09:22 PM
Newbie
Join Date: January 17th, 2005
Posts: 10
What about reading until you get to line100, writing all lines back to a new file,
and then write the new line to the new file,
then read and write all remaining lines,
then move the new file over to the old
file name. Just one way of doing it fast.
There's probably heaps of ways of doing it.
What type of program is it and what os will
it run on?

Do you know any VB or do you know any VB
programmers? I need help writing an animation app. Have a look at...
http://www.bri.net.au/~plethora

Geoff Willliams
plethora@bri.net.au
  #3  
Old August 21st, 2002, 10:58 PM
Newbie
Join Date: January 17th, 2005
Posts: 10
Here's another way that might work, just like yours but simplified, see if it does the same thing...

$myline = 100; # the line I want to edit
$mynewline = "Hello I am a new line that has been replaced.";

open( DATA, "data.txt" );
@data = < DATA >; # Get data
close( DATA );
$data[$myline]=$mynewline; # Make the change
open( DATA, ">data.txt" );
foreach $line (@data){
print DATA $line; # Write the data file to disk
}
close(DATA);

You'd have to test it though, I think I left out out /n after the newlinetext goes in.

But it shows that the file writing and reading should be done a lot quicker. Better to have the swap and the conditional outside of the I/O part.


Geoff.
Join ABW to remove this sponsored message.
  #4  
Old August 22nd, 2002, 02:21 AM
c4 c4 is offline
c4's Avatar
Full Member
Join Date: January 17th, 2005
Posts: 488
Hi!

Thanks for reply! I already got this working pretty much the same way you suggested maybe just a line of code less [img]/infopop/emoticons/icon_smile.gif[/img]


$myline = 100; # the line I want to edit
$mynewline = "Hello I am a new line that has been replaced.";

open( DATA, "data.txt" );
@data = < DATA >; # Get data
close( DATA );
$data[$myline]=$mynewline; # Make the change
open( DATA, ">data.txt" );
print DATA @data; # Write the data file to disk
close(DATA);

It's the same I'm just not re-writting the file line by line. (Note that print DATA @data; must be used, not print data "@data"; since this second one ads spaces to the beginning of every new line)


VB programmers? Sorry, can't think of anyone ...

Regards

## UBB = !%#)&$=)"%/!"#&#=?*"!*)=*)#ยจ!"

[ 08-22-2002: Message edited by: c4 ]
Reply
 

Tools Search
Search:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump

X

Welcome to ABestWeb.com

Create your username to jump into the discussion!

ABestWeb.com is the largest affiliate marketing community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?



Content Relevant URLs by vBSEO ©2011, Crawlability, Inc.