delete first line of a text file

M

Michele Zio

hi,
how can i delete first line of a file?

example:

file.txt:
1
2
3
4

after operation:

file.txt:
2
3
4


thanks!
 
A

Adam Shelly

hi,
how can i delete first line of a file?
Assuming it's an ASCII file, this should work: (Although it might be
slow on very large files)

FILENAME="file.txt"
text=''
File.open(FILENAME,"r"){|f|f.gets;text=f.read}
File.open(FILENAME,"w+"){|f| f.write(text)}

-Adam
 
R

Rodrigo Bermejo

Michele said:
hi,
how can i delete first line of a file?

example:

file.txt:
1
2
3
4

after operation:

file.txt:
2
3
4


thanks!

File.readlines("file.txt")[0..-2]

-r.
 
M

Michele Zio

Adam said:
Assuming it's an ASCII file, this should work: (Although it might be
slow on very large files)

FILENAME="file.txt"
text=''
File.open(FILENAME,"r"){|f|f.gets;text=f.read}
File.open(FILENAME,"w+"){|f| f.write(text)}

-Adam

it's a very small file :)
it works very well, thanks a lot!!!!!

michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top