delete last line - SCO

This is a discussion on delete last line - SCO ; how can I delete the last line of a text file from the command line ? I mean only last line...

+ Reply to Thread
Results 1 to 12 of 12

Thread: delete last line

  1. delete last line

    how can I delete the last line of a text file from the command line ?

    I mean only last line


  2. Re: delete last line

    pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    | how can I delete the last line of a text file from the command line ?
    |
    | I mean only last line

    sed '$d' in.file > out.file

    --
    JP

  3. Re: delete last line

    On Thu, Jan 10, 2008, Jean-Pierre Radley wrote:
    >pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    >| how can I delete the last line of a text file from the command line ?
    >|
    >| I mean only last line
    >
    > sed '$d' in.file > out.file


    In place edit using the ex editor (would probably work with ed as well.

    ex - filename < $d
    w
    q
    DONE

    Using the gnu shtool program:

    shtool subst -e '$d' filename

    Bill
    --
    INTERNET: bill@celestial.com Bill Campbell; Celestial Software LLC
    URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way
    FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676

    The only freedom which deserves the name, is that of pursuing our own good
    in our own way, so long as we do not attempt to deprive others of theirs,
    or impede their efforts to obtain it. -- John Stuart Mill, 1859

  4. Re: delete last line

    Jean-Pierre Radley typed (on Thu, Jan 10, 2008 at 05:55:06PM -0500):
    | pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    | | how can I delete the last line of a text file from the command line ?
    | |
    | | I mean only last line
    |
    | sed '$d' in.file > out.file
    |
    | --
    | JP

    Well... now I'm curious.

    How would you delete ONLY the next-to-last line,
    leaving the previous very last line to replace
    the space where the next-to-last line was just
    removed?

    Jeff H

  5. Re: delete last line

    Jeff Hyman typed (on Fri, Jan 11, 2008 at 11:20:40AM -0500):
    | Jean-Pierre Radley typed (on Thu, Jan 10, 2008 at 05:55:06PM -0500):
    | | pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    | | | how can I delete the last line of a text file from the command line ?
    | | |
    | | | I mean only last line
    | |
    | | sed '$d' in.file > out.file
    | |
    | | --
    | | JP
    |
    | Well... now I'm curious.
    |
    | How would you delete ONLY the next-to-last line,
    | leaving the previous very last line to replace
    | the space where the next-to-last line was just
    | removed?

    Badly phrased question. If you remove a line, there is no "space left
    behind". There would only be a "space left behind" if you deleted the
    just the line's visible contents, AKA as blanking the line. When one
    says remove, one means removing the 'newline' as well, so that there's
    nothing left at all.

    In any case, what you meant to do would want a small modification to what
    Bill just posted:


    ex - filename < $
    -
    w
    q
    DONE

    --
    JP

  6. Re: delete last line

    On Fri, Jan 11, 2008, Jean-Pierre Radley wrote:
    >Jeff Hyman typed (on Fri, Jan 11, 2008 at 11:20:40AM -0500):
    >| Jean-Pierre Radley typed (on Thu, Jan 10, 2008 at 05:55:06PM -0500):
    >| | pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    >| | | how can I delete the last line of a text file from the command line ?
    >| | |
    >| | | I mean only last line
    >| |
    >| | sed '$d' in.file > out.file
    >| |
    >| | --
    >| | JP
    >|
    >| Well... now I'm curious.
    >|
    >| How would you delete ONLY the next-to-last line,
    >| leaving the previous very last line to replace
    >| the space where the next-to-last line was just
    >| removed?
    >
    >Badly phrased question. If you remove a line, there is no "space left
    >behind". There would only be a "space left behind" if you deleted the
    >just the line's visible contents, AKA as blanking the line. When one
    >says remove, one means removing the 'newline' as well, so that there's
    >nothing left at all.
    >
    >In any case, what you meant to do would want a small modification to what
    >Bill just posted:
    >
    >
    > ex - filename < > $
    > -
    > w
    > q
    > DONE


    Or more easily:

    ex - filename < $-1d
    w
    q
    DONE

    To delete 10 lines prior to the last line, leaving the last line:

    ex - filename < $-11,$-1d
    w
    q
    DONE

    Bill
    --
    INTERNET: bill@celestial.com Bill Campbell; Celestial Software LLC
    URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way
    FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676

    "If taxation without consent is not robbery, then any band of robbers
    have only to declare themselves a government, and all their robberies
    are legalized." -- Lysander Spooner, Letter to Grover Cleveland 1886

  7. Re: delete last line

    Jean-Pierre Radley typed (on Fri, Jan 11, 2008 at 12:25:38PM -0500):
    | Jeff Hyman typed (on Fri, Jan 11, 2008 at 11:20:40AM -0500):
    | | Jean-Pierre Radley typed (on Thu, Jan 10, 2008 at 05:55:06PM -0500):
    | | | pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    | | | | how can I delete the last line of a text file from the command line ?
    | | | |
    | | | | I mean only last line
    | | |
    | | | sed '$d' in.file > out.file
    | | |
    | | | --
    | | | JP
    | |
    | | Well... now I'm curious.
    | |
    | | How would you delete ONLY the next-to-last line,
    | | leaving the previous very last line to replace
    | | the space where the next-to-last line was just
    | | removed?
    |
    | Badly phrased question. If you remove a line, there is no "space left
    | behind". There would only be a "space left behind" if you deleted the
    | just the line's visible contents, AKA as blanking the line. When one
    | says remove, one means removing the 'newline' as well, so that there's
    | nothing left at all.
    |
    | In any case, what you meant to do would want a small modification to what
    | Bill just posted:
    |
    | ex - filename < | $
    | -
    | w
    | q
    | DONE
    |

    JP,
    We've know each other for a long long time. If you were able
    to "fix" me from making all those typos, confusing explainations,
    questionable questions, etc, I would have had a sex change and tried
    to marry you long ago. Please don't mention this to Jane. For those
    of that don't know me, I'm a mans man, so don't even think about what
    your thinking .

    The one hyphen is the difference between what you posted above
    and what Bill posted. So, what would be the case if:
    ###---------------------------------------------------
    ex - filename < $
    - Removes next to last line (from previous post)
    - Add another hyphen to rm the next-to-next last line ?
    Add 5 more to remove another 5 lines ?
    w
    q
    DONE
    ###---------------------------------------------------
    IOW: What if one wanted to remove the last 6 lines, but
    keep the very last line? I understand if your to busy,
    but I find this issue really interesting for some reason.
    ###---------------------------------------------------

    - Jeff H


  8. Re: delete last line

    Bill Campbell typed (on Fri, Jan 11, 2008 at 09:47:37AM -0800):
    | On Fri, Jan 11, 2008, Jean-Pierre Radley wrote:
    | >Jeff Hyman typed (on Fri, Jan 11, 2008 at 11:20:40AM -0500):
    | >| Jean-Pierre Radley typed (on Thu, Jan 10, 2008 at 05:55:06PM -0500):
    | >| | pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    | >| | | how can I delete the last line of a text file from the command line ?
    | >| | |
    | >| | | I mean only last line
    | >| |
    | >| | sed '$d' in.file > out.file
    | >| |
    | >| | --
    | >| | JP
    | >|
    | >| Well... now I'm curious.
    | >|
    | >| How would you delete ONLY the next-to-last line,
    | >| leaving the previous very last line to replace
    | >| the space where the next-to-last line was just
    | >| removed?
    | >
    | >Badly phrased question. If you remove a line, there is no "space left
    | >behind". There would only be a "space left behind" if you deleted the
    | >just the line's visible contents, AKA as blanking the line. When one
    | >says remove, one means removing the 'newline' as well, so that there's
    | >nothing left at all.
    | >
    | >In any case, what you meant to do would want a small modification to what
    | >Bill just posted:
    | >
    | >
    | > ex - filename < | > $
    | > -
    | > w
    | > q
    | > DONE
    |
    | Or more easily:
    |
    | ex - filename < | $-1d
    | w
    | q
    | DONE
    |
    | To delete 10 lines prior to the last line, leaving the last line:
    |
    | ex - filename < | $-11,$-1d
    | w
    | q
    | DONE
    |
    | Bill

    I just replied to JP... and your reply here is the answer I was
    curious about. Bill... you are the man!
    JP, please disregard my last statement. I really don't
    like you "that" much.
    - Jeff H

  9. Re: delete last line

    On 10 ene, 23:55, Jean-Pierre Radley wrote:
    > pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    > | how can I delete the last line of a text file from the command line ?
    > |
    > | I mean only last line
    >
    > * * * * sed '$d' in.file > out.file
    >
    > --
    > JP


    JP, thanks

    in fact this is not working to me because my file is a DOS file ,

    I really only want to remove final Ctrl Z character .

    I am using xtod , because I need this dos file , but without final
    Ctrl Z .

    any additional help ?

    Thanks

    pablo

  10. Re: delete last line

    pablo typed (on Fri, Jan 18, 2008 at 01:03:17PM -0800):
    | On 10 ene, 23:55, Jean-Pierre Radley wrote:
    | > pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    | > | how can I delete the last line of a text file from the command line ?
    | > |
    | > | I mean only last line
    | >
    | > * * * * sed '$d' in.file > out.file
    | >
    | > --
    | > JP
    |
    | JP, thanks
    |
    | in fact this is not working to me because my file is a DOS file ,
    |
    | I really only want to remove final Ctrl Z character .
    |
    | I am using xtod , because I need this dos file , but without final
    | Ctrl Z .
    |
    | any additional help ?
    |
    | Thanks
    |
    | pablo

    You didn't say that the first time, and you still are not clearly saying
    what you want. Are you starting with a Unix text file to which you want
    to add Carriage-returns at the end of each line? Xtod will do this, but
    also adds the ^Z. You can filter it out with

    xtod inputfile | grep -v 'Ctrl-Z' > outputfile

    (You might need to type that as

    grep -v 'Ctrl-VCtrl-Z'
    ).

    Or you could forget xtod and filter your source file through my little
    cr+- script, which also can reverse its action so as to remove Carriage
    Returns from a file which has them.


    : cr+-
    # adds CRs if not present, strips them if present
    # note: if you just cat this file, you miss its essence
    # if you cat -v this file, you see the _real_ ^Ms in it
    #
    sed '
    s---g
    t
    s+$++
    t
    '

    --
    JP

  11. Re: delete last line

    On 18 ene, 22:35, Jean-Pierre Radley wrote:
    > pablo typed (on Fri, Jan 18, 2008 at 01:03:17PM -0800):
    > | On 10 ene, 23:55, Jean-Pierre Radley wrote:
    > | > pablo typed (on Thu, Jan 10, 2008 at 02:03:03PM -0800):
    > | > | how can I delete the last line of a text file from the command line ?
    > | > |
    > | > | I mean only last line
    > | >
    > | > * * * * sed '$d' in.file > out.file
    > | >
    > | > --
    > | > JP
    > |
    > | JP, thanks
    > |
    > | in fact this is not working to me because my file is a DOS file ,
    > |
    > | I really only want to remove final Ctrl Z * character .
    > |
    > | I am using xtod , because *I need this dos file , but without final
    > | Ctrl Z .
    > |
    > | any additional help ?
    > |
    > | Thanks
    > |
    > | pablo
    >
    > You didn't say that the first time, and you still are not clearly saying
    > what you want. *Are you starting with a Unix text file to which you want
    > to add Carriage-returns at the end of each line? *Xtod will do this, but
    > also adds the ^Z. *You can filter it out with
    >
    > * * * * xtod inputfile | grep -v 'Ctrl-Z' > outputfile
    >
    > (You might need to type that as
    >
    > * * * * grep -v 'Ctrl-VCtrl-Z'
    > ).
    >
    > Or you could forget xtod and filter your source file through my little
    > cr+- script, which also can reverse its action so as to remove Carriage
    > Returns from a file which has them.
    >
    > * * * * : cr+-
    > * * * * # adds CRs if not present, strips them if present
    > * * * * # note: if you just cat this file, you miss its essence
    > * * * * # if you cat -v this file, you see the _real_ ^Ms in it
    > * * * * #
    > * * * * sed '
    > * * * * s- --g
    > * * * * t
    > * * * * s+$+ +
    > * * * * t
    > * * * * '
    >
    > --
    > JP


    THAT's WODERFULL

    I use octal code instead:

    grep -v '\0032' QUA58.DOS > QUA58.TXT
    cp QUA58.TXT QUA58.DOS

    Now I would like this in one line , without intermediate QUA58.TXT :
    IS POSSIBLE ?

    Thanks and regards

    pablo

  12. Re: delete last line

    pablo typed (on Mon, Jan 21, 2008 at 01:05:42PM -0800):
    | THAT's WODERFULL
    |
    | I use octal code instead:
    |
    | grep -v '\0032' QUA58.DOS > QUA58.TXT
    | cp QUA58.TXT QUA58.DOS
    |
    | Now I would like this in one line , without intermediate QUA58.TXT :
    | IS POSSIBLE ?

    It is most certainly possible! You need to learn shell scripting, but
    I'm not going to teach it here. Go look up 'overwrite' in Kernighan
    & Pike; or instead of 'grep', use 'ed' or 'edit' in a here document
    construct (of which Bill Campbell already provided an example).

    --
    JP

+ Reply to Thread