deleting first line from a file

S

suresh

Hi

I have a HUGE data file multiple lines of data. I want to delete just
the first line from it. How to do it efficiently?

thanks
suresh
 
C

Chris Shea

Hi

I have a HUGE data file multiple lines of data. I want to delete just
the first line from it. How to do it efficiently?

thanks
suresh

Would this work for you?

tail -n +2 original.file > modified.file

HTH,
Chris
 
D

David Masover

Hi

I have a HUGE data file multiple lines of data. I want to delete just
the first line from it. How to do it efficiently?

Depends what you mean by "efficiently".

Removing data from the end of a file takes close to no time at all, if you do
it right -- it just requires truncating the file.

Removing data from the beginning of a file, or the middle of a file, isn't
something most filesystems will let you do. As Chris said, you're going to
have to read the entire file in (minus the line you want removed) and output
it to another file. (Or rather, that's what his tail command does.)

So, if we're talking about a multi-gigabyte file, it's going to take a few
minutes.
 
S

suresh

Would this work for you?

tail -n +2 original.file > modified.file

HTH,
Chris

Hi

BTW is there any equivalent method equivalent to linux tail in ruby?

suresh
 
H

Heesob Park

2008/6/4 suresh said:
Hi

BTW is there any equivalent method equivalent to linux tail in ruby?

ruby -n -e 'print $_ if $.>1' original.file > modified.file

Regards,

Park Heesob
 
H

Heesob Park

Hi,

2008/6/4 suresh said:
Hi Park Heesob

Thanks. But how can this be done inside a .rb file? The above must be
from command line right?
It is equivalent to

while gets
print $_ if $.>1
end

Regards,

Park Heesob
 
D

David Masover

BTW is there any equivalent method equivalent to linux tail in ruby?

Not really, but it shouldn't be difficult to build. Maybe trickier to build
efficiently, though.
 
S

Suraj Kurapati

Chris said:
tail -n +2 original.file > modified.file

For the sake of trivia, sed (stream editor) also does the job:

sed 1d original.file > modified.file # 1d means delete line 1
 
P

Peña, Botp

From: suresh [mailto:[email protected]]=20
# I have a HUGE data file multiple lines of data. I want to delete just
# the first line from it. How to do it efficiently?

i think you want something fast yet clean ruby solution.

maybe something like,

botp@botp-desktop:~$ cat test.txt
this line will be deleted
2nd line
3rd asdfasdf line
4th qwerty line
5th
6th six
7th 777777777777

botp@botp-desktop:~$ irb
irb(main):001:0> File.open("newfile","w") do |fw|
irb(main):002:1* File.open("test.txt") do |fr|
irb(main):003:2* fr.gets
irb(main):004:2> fw.write fr.read
irb(main):005:2> end
irb(main):006:1> end
=3D> 72

botp@botp-desktop:~$ cat newfile
2nd line
3rd asdfasdf line
4th qwerty line
5th
6th six
7th 777777777777


just add some checks/rescues so that it will work on all your cases.

kind regards -botp
 
R

Rob Biedenharn

If that's all you want (and you're not frightened by the command-line =20=

options):

ruby -i -n -e 'puts $_ if $. > 1' lines.txt

Of if you want to keep the original in lines.txt.orig

ruby -i.orig -n -e 'puts $_ if $. > 1' lines.txt

-Rob

From: suresh [mailto:[email protected]]
# I have a HUGE data file multiple lines of data. I want to delete =20
just
# the first line from it. How to do it efficiently?

i think you want something fast yet clean ruby solution.

maybe something like,

botp@botp-desktop:~$ cat test.txt
this line will be deleted
2nd line
3rd asdfasdf line
4th qwerty line
5th
6th six
7th 777777777777

botp@botp-desktop:~$ irb
irb(main):001:0> File.open("newfile","w") do |fw|
irb(main):002:1* File.open("test.txt") do |fr|
irb(main):003:2* fr.gets
irb(main):004:2> fw.write fr.read
irb(main):005:2> end
irb(main):006:1> end
=3D> 72

botp@botp-desktop:~$ cat newfile
2nd line
3rd asdfasdf line
4th qwerty line
5th
6th six
7th 777777777777


just add some checks/rescues so that it will work on all your cases.

kind regards -botp

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Jimmy Kofler

suresh said:
Hi

I have a HUGE data file multiple lines of data. I want to delete just
the first line from it. How to do it efficiently?

thanks
suresh

csplit file 1 '{1}'

sed -i "" '1d' file # in-place without backup

Cheers,

j.k.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top