Delete starting and ending lines of a file

Discussion in 'Perl' started by Srinivasa T.N., Nov 28, 2003.

  1. Hi,
    I want to delete the first 3 lines and last 3 lines of a file. How
    can I do it? I want to repeat on multiple files..

    TIA

    Regards,
    Seenu.
     
    Srinivasa T.N., Nov 28, 2003
    #1
    1. Advertising

  2. "Srinivasa T.N." wrote:
    >
    > Hi,
    > I want to delete the first 3 lines and last 3 lines of a file. How
    > can I do it? I want to repeat on multiple files..
    >
    > TIA
    >
    > Regards,
    > Seenu.



    tail +4 <filename> | head -3
     
    Geezer From The Freezer, Nov 28, 2003
    #2
    1. Advertising

  3. Srinivasa T.N. wrote:
    > I want to delete the first 3 lines and last 3 lines of a file. How
    > can I do it?


    This Question is Asked Frequently. Please see "perldoc -q delete":
    "How do I change one line in a file/delete a line in a file/insert a
    line in the middle of a file/append to the beginning of a file?"

    > I want to repeat on multiple files..


    Please see "perldoc File::Find".

    jue
     
    Jürgen Exner, Nov 28, 2003
    #3
  4. (Srinivasa T.N.) wrote in message
    >
    > I want to delete the first 3 lines and last 3 lines of a file. How
    > can I do it? I want to repeat on multiple files..
    >


    sed -e '
    1,4{
    4!d
    $!N;$!N
    }
    $d
    N;P;D
    ' yourfile
     
    rakesh sharma, Nov 28, 2003
    #4
  5. In article <>,
    Srinivasa T.N. <> wrote:
    >Hi,
    > I want to delete the first 3 lines and last 3 lines of a file. How
    >can I do it? I want to repeat on multiple files..
    >
    >TIA
    >
    >Regards,
    >Seenu.


    tail +4 infile | tac | tail +4 | tac

    assuming that tac is available.


    Chuck Demas

    --
    Eat Healthy | _ _ | Nothing would be done at all,
    Stay Fit | @ @ | If a man waited to do it so well,
    Die Anyway | v | That no one could find fault with it.
    | \___/ | http://world.std.com/~cpd
     
    Charles Demas, Nov 28, 2003
    #5
  6. Srinivasa T.N.

    Nick Santos Guest

    "Srinivasa T.N." <> wrote in message
    news:...
    > Hi,
    > I want to delete the first 3 lines and last 3 lines of a file. How
    > can I do it? I want to repeat on multiple files..
    >
    > TIA
    >
    > Regards,
    > Seenu.


    I'm obviously not as experienced as the other people that have responded,
    and didn't quite understand their methods so thought maybe you may not
    (though you may - I dunno where everyone's at, I'm new to the newsgroup), so
    I thought I'd share my method.

    #begin code

    open (FIL, "yourfile");
    @array=<FIL>; #read the file into an array
    close (FIL);
    $end=@array; #total in array

    $end=$end-3; #to delete the last lines
    $i=3; #and the first

    open (FIL, ">yourfile"); #reopen file for writing

    while($i<$end){
    print FIL "$array[$i]\n";
    }
    close (FIL);

    #end code

    I haven't tested it, and it may very well have errors in syntax (or other
    functionality related items) but that should do what you want and not
    require any extra libraries
     
    Nick Santos, Nov 29, 2003
    #6
  7. Nick Santos wrote:
    > "Srinivasa T.N." <> wrote in message
    > news:...
    >> I want to delete the first 3 lines and last 3 lines of a file.
    >> How can I do it? I want to repeat on multiple files..

    >
    > open (FIL, "yourfile");
    > @array=<FIL>; #read the file into an array
    > close (FIL);
    > $end=@array; #total in array
    >
    > $end=$end-3; #to delete the last lines
    > $i=3; #and the first
    >
    > open (FIL, ">yourfile"); #reopen file for writing
    >
    > while($i<$end){
    > print FIL "$array[$i]\n";
    > }


    You change neither $i nor $end, so your loop will never terminate. Probably
    you just forgot a
    $i++;
    somewhere.

    But you can replace this whole while loop with a simple array slice
    print FIL @array[3..$#array-3];
    Then you don't even need those auxilliary variables $end or $i.
    Please be careful, I didn't test it either and it may have a one-off error
    for the upper bound.
    And of course it may have unexpected results if the array contains less then
    6 elements.

    > close (FIL);


    jue
     
    Jürgen Exner, Nov 29, 2003
    #7
  8. Srinivasa T.N.

    Amy G Guest

    You definatly want to do it using tail +4 <file> | head -3

    The tail command prints the last few lines of the file. A tail -10 prints
    the last ten lines of a file. A tail +4 prints all but the last three lines
    of the file. Much the same, head -3 prints everything from the fourth line
    on. The | is a pipe which passes the information from the first part of the
    command to the second.


    "Nick Santos" <> wrote in message
    news:efVxb.250784$ao4.892339@attbi_s51...
    >
    > "Srinivasa T.N." <> wrote in message
    > news:...
    > > Hi,
    > > I want to delete the first 3 lines and last 3 lines of a file. How
    > > can I do it? I want to repeat on multiple files..
    > >
    > > TIA
    > >
    > > Regards,
    > > Seenu.

    >
    > I'm obviously not as experienced as the other people that have responded,
    > and didn't quite understand their methods so thought maybe you may not
    > (though you may - I dunno where everyone's at, I'm new to the newsgroup),

    so
    > I thought I'd share my method.
    >
    > #begin code
    >
    > open (FIL, "yourfile");
    > @array=<FIL>; #read the file into an array
    > close (FIL);
    > $end=@array; #total in array
    >
    > $end=$end-3; #to delete the last lines
    > $i=3; #and the first
    >
    > open (FIL, ">yourfile"); #reopen file for writing
    >
    > while($i<$end){
    > print FIL "$array[$i]\n";
    > }
    > close (FIL);
    >
    > #end code
    >
    > I haven't tested it, and it may very well have errors in syntax (or other
    > functionality related items) but that should do what you want and not
    > require any extra libraries
    >
    >
     
    Amy G, Dec 1, 2003
    #8
    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. Murali
    Replies:
    2
    Views:
    606
    Jerry Coffin
    Mar 9, 2006
  2. Jona
    Replies:
    9
    Views:
    446
    Jim Langston
    May 10, 2007
  3. Mayeul
    Replies:
    0
    Views:
    1,004
    Mayeul
    Jul 11, 2011
  4. Joe Kesselman
    Replies:
    2
    Views:
    909
    Joe Kesselman
    Jul 12, 2011
  5. Mayeul
    Replies:
    0
    Views:
    996
    Mayeul
    Jul 12, 2011
Loading...

Share This Page