Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Open file, get first line, delete first line close file

Reply
Thread Tools

Open file, get first line, delete first line close file

 
 
Richard Schneeman
Guest
Posts: n/a
 
      08-23-2008
Hey, i'm trying to open a file, get the first line of the file, delete
that line from the file, and then close the file. Using ruby 1.8.6.

I've tried using

The_File = IO.readlines("public/languages/chinese/practice.txt")

&

The_File = open('public/languages/chinese/practice.txt','r+')

but i can't figure out the correct syntax for how to delete a line in a
file, and then save that file.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Daniel Bush
Guest
Posts: n/a
 
      08-23-2008
Richard Schneeman wrote:
> Hey, i'm trying to open a file, get the first line of the file, delete
> that line from the file, and then close the file. Using ruby 1.8.6.
>
> I've tried using
>
> The_File = IO.readlines("public/languages/chinese/practice.txt")
>
> &
>
> The_File = open('public/languages/chinese/practice.txt','r+')
>
> but i can't figure out the correct syntax for how to delete a line in a
> file, and then save that file.


Simplest thing is to get it into memory like you did with readlines
above and write out the altered contents to a new file and then move it.
(I prefer File.readlines - although this is the same method either way).
Large files might require different treatment rather than slurping into
memory like that.

That being said, I'd be curious to hear how people use r+ mode.

Daniel
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Richard Schneeman
Guest
Posts: n/a
 
      08-23-2008



That is one option...the reason i need to be able to delete lines, is
that i am running a rake task on a server, that i cannot easily modify
files, and I can't predict how long the rake task will time out. The
task takes entries from text based dictionaries and adds it to my DB,
the thing is, if i didn't delete the lines i've already added, everytime
i ran the task, (it has to be run multiple times due to timeouts) i
would only re-add the same lines. The deletion acts as a place holder of
sorts. I'll play around with your suggestion, and i'll let you know. If
anyone else has an alternate method...i'm all ears

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Richard Schneeman
Guest
Posts: n/a
 
      08-23-2008
Richard Schneeman wrote:
>
>
>
> That is one option...the reason i need to be able to delete lines, is
> that i am running a rake task on a server, that i cannot easily modify
> files, and I can't predict how long the rake task will time out. The
> task takes entries from text based dictionaries and adds it to my DB,
> the thing is, if i didn't delete the lines i've already added, everytime
> i ran the task, (it has to be run multiple times due to timeouts) i
> would only re-add the same lines. The deletion acts as a place holder of
> sorts. I'll play around with your suggestion, and i'll let you know. If
> anyone else has an alternate method...i'm all ears


Its gross but it works

active_dictionary =
File.readlines("public/languages/chinese/practice.txt")

open('public/languages/chinese/practice.txt', 'w') do |file|
file.puts active_dictionary[1,active_dictionary.size]
end

Once again, i'm interested in different approaches, or something, not
quite so processor intensive
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Daniel Bush
Guest
Posts: n/a
 
      08-24-2008
Richard Schneeman wrote:
...
> Its gross but it works


I don't know if it's all that gross. I have feeling this is
standard way to do it for apps and editors ie write out entire altered
content
after working with file in-memory using whatever scheme.
Different story if you're a database I guess.

Here is one scheme some text editors use:
http://en.wikipedia.org/wiki/Gap_buffer
although it doesn't discuss file system/persistence issues.
Presumably when you hit the save button, the system writes
out to a new file (the gap buffer is not playing around with
the old file stream).

If you're just replacing stuff character for character, then
it seems ok to use the file stream (in r+ mode) or if you're
appending (or both); but deleting or inserting content seems
problematic - not sure it's possible let alone standardized.
Anyone want to weigh in here?


> Once again, i'm interested in different approaches, or something, not
> quite so processor intensive


If the file is really large, you can perhaps just move through the
stream till you get to the point where you want to start
then commence writing from the old stream to the new file stream.
May be ways to optimise it.

Sparse files and fixed line lengths ?
http://en.wikipedia.org/wiki/Sparse_file

Maybe I've said enough wrong things to provoke a reacion
from someone else.

Daniel
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Erik Hollensbe
Guest
Posts: n/a
 
      08-24-2008
Richard Schneeman wrote:
> but i can't figure out the correct syntax for how to delete a line in a
> file, and then save that file.


tail +2 the_file > the_new_file

Not all problems are best solved with ruby

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Dave Bass
Guest
Posts: n/a
 
      08-24-2008
Erik Hollensbe wrote:
> Not all problems are best solved with ruby


And sometimes the problems are not with the program but with the data
structure -- maybe a flat file isn't the right way to do things?

And... it's a lot easier to delete the *last* line of a file than the
first.

Just some thoughts.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Erik Hollensbe
Guest
Posts: n/a
 
      08-25-2008
Dave Bass wrote:
> And... it's a lot easier to delete the *last* line of a file than the
> first.


I don't think this is actually true, can you explain further?

-Erik
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      08-25-2008
On Aug 25, 2008, at 4:07 AM, Erik Hollensbe wrote:

> Dave Bass wrote:
>> And... it's a lot easier to delete the *last* line of a file than the
>> first.

>
> I don't think this is actually true, can you explain further?
>
> -Erik



You can just truncate the file size. You don't have any subsequent
lines (bytes) to move into a new position within the file.

-Rob

Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
Erik Hollensbe
Guest
Posts: n/a
 
      08-25-2008
Rob Biedenharn wrote:
> On Aug 25, 2008, at 4:07 AM, Erik Hollensbe wrote:
>
>> Dave Bass wrote:
>>> And... it's a lot easier to delete the *last* line of a file than the
>>> first.

>>
>> I don't think this is actually true, can you explain further?
>>
>> -Erik

>
>
> You can just truncate the file size. You don't have any subsequent
> lines (bytes) to move into a new position within the file.


How do you know which line is the last line?

Unless there's something I don't know, that involves reading the whole
file, or a combination of seek/read from the end until you find the last
newline, which is essentially what tail +2 does, but starts at the
beginning of the file.

"Moving" data in a file is the worst possible scenario for I/O at all.
You can do both of these operations in a single pass read of the file
without shoving the whole thing into memory at once. It just involves
writing to one file and reading from another, is all.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close a TCP socket? (TCPSocket#close doesn't close it) Iñaki Baz Castillo Ruby 7 01-12-2010 01:32 PM
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
is better to open, write, close file than open, write, append, close? Iulian Ilea Javascript 1 12-21-2006 04:32 PM
Delete first line from file Tor Erik Sønvisen Python 4 03-04-2005 04:15 PM
ftplib - file.write, file.close() - verify finished writing locally before delete source? Peter A. Schott Python 0 02-10-2005 10:22 PM



Advertisments