deleting first line from a file

Discussion in 'Ruby' started by suresh, Jun 3, 2008.

  1. suresh

    suresh Guest

    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
    suresh, Jun 3, 2008
    #1
    1. Advertising

  2. suresh

    Chris Shea Guest

    On Jun 3, 1:59 pm, suresh <> wrote:
    > 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
    Chris Shea, Jun 3, 2008
    #2
    1. Advertising

  3. On Tuesday 03 June 2008 14:59:17 suresh wrote:
    > 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.
    David Masover, Jun 3, 2008
    #3
  4. suresh

    suresh Guest

    On Jun 4, 1:48 am, Chris Shea <> wrote:
    > On Jun 3, 1:59 pm, suresh <> wrote:
    >
    > > 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


    Hi Chris

    Thanks, I was not aware of the +2 option...

    suresh
    suresh, Jun 4, 2008
    #4
  5. suresh

    suresh Guest

    On Jun 4, 1:48 am, Chris Shea <> wrote:
    > On Jun 3, 1:59 pm, suresh <> wrote:
    >
    > > 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


    Hi

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

    suresh
    suresh, Jun 4, 2008
    #5
  6. suresh

    Heesob Park Guest

    2008/6/4 suresh <>:
    > On Jun 4, 1:48 am, Chris Shea <> wrote:
    >> On Jun 3, 1:59 pm, suresh <> wrote:
    >>
    >> > 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

    >
    > 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
    Heesob Park, Jun 4, 2008
    #6
  7. suresh

    suresh Guest

    On Jun 4, 3:19 pm, Heesob Park <> wrote:
    > 2008/6/4 suresh <>:
    >
    >
    >
    > > On Jun 4, 1:48 am, Chris Shea <> wrote:
    > >> On Jun 3, 1:59 pm, suresh <> wrote:

    >
    > >> > 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

    >
    > > 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


    Hi Park Heesob

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

    suresh
    suresh, Jun 4, 2008
    #7
  8. suresh

    Heesob Park Guest

    Hi,

    2008/6/4 suresh <>:
    > On Jun 4, 3:19 pm, Heesob Park <> wrote:
    >> 2008/6/4 suresh <>:
    >>
    >>
    >>
    >> > On Jun 4, 1:48 am, Chris Shea <> wrote:
    >> >> On Jun 3, 1:59 pm, suresh <> wrote:

    >>
    >> >> > 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

    >>
    >> > 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

    >
    > 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
    Heesob Park, Jun 4, 2008
    #8
  9. suresh

    suresh Guest

    On Jun 4, 5:20 pm, Heesob Park <> wrote:
    > Hi,
    >
    > 2008/6/4 suresh <>:
    >
    > > On Jun 4, 3:19 pm, Heesob Park <> wrote:
    > >> 2008/6/4 suresh <>:

    >
    > >> > On Jun 4, 1:48 am, Chris Shea <> wrote:
    > >> >> On Jun 3, 1:59 pm, suresh <> wrote:

    >
    > >> >> > 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

    >
    > >> > 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

    >
    > > 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


    Thanks Park Heesob, thank you
    suresh
    suresh, Jun 4, 2008
    #9
  10. On Wednesday 04 June 2008 04:59:12 suresh wrote:

    > 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.
    David Masover, Jun 4, 2008
    #10
  11. Chris Shea wrote:
    > On Jun 3, 1:59 pm, suresh <> wrote:
    >> 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?
    >>

    > 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
    --
    Posted via http://www.ruby-forum.com/.
    Suraj Kurapati, Jun 4, 2008
    #11
  12. suresh

    Peña, Botp Guest

    From: suresh [mailto:]=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
    Peña, Botp, Jun 5, 2008
    #12
  13. 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

    On Jun 4, 2008, at 9:16 PM, Pe=F1a, Botp wrote:

    > From: suresh [mailto:]
    > # 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
    Rob Biedenharn, Jun 5, 2008
    #13
  14. On Jun 4, 11:57 am, David Masover <> wrote:
    > On Wednesday 04 June 2008 04:59:12 suresh wrote:
    >
    > > 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.


    There's no core method, but there is a library:

    http://raa.ruby-lang.org/project/file-tail/

    Regards,

    Dan
    Daniel Berger, Jun 5, 2008
    #14
  15. suresh

    Jimmy Kofler Guest

    > suresh wrote:
    > 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.
    --
    Posted via http://www.ruby-forum.com/.
    Jimmy Kofler, Jun 5, 2008
    #15
  16. suresh

    Jimmy Kofler Guest

    Jimmy Kofler, Jun 5, 2008
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harry Barker
    Replies:
    2
    Views:
    513
    Alf P. Steinbach
    Apr 19, 2006
  2. scad
    Replies:
    23
    Views:
    1,162
    Alf P. Steinbach
    May 17, 2009
  3. pozz

    Deleting first N lines from a text file

    pozz, Nov 15, 2011, in forum: C Programming
    Replies:
    26
    Views:
    1,967
    Dr Nick
    Dec 16, 2011
  4. Richard Schneeman
    Replies:
    16
    Views:
    483
    Daniel Bush
    Aug 27, 2008
  5. crea
    Replies:
    2
    Views:
    409
    Nobody
    Dec 28, 2012
Loading...

Share This Page