How to pop (read&remove) a line of a file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I am trying to get the last line of a file and then delete it:



Read the line:



sed -n '$p' file
# or
tail -n 1 file


Remove the line:



sed -i '$d' file


This works, but is there a way to do this with just a single command ?










share|improve this question























  • I'm not focused on sed. Any default tool will be fine.
    – RoVo
    29 mins ago











  • In what way does sed not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular way sed is not good enough. Or, do you want to perform both operations in one command?
    – Kusalananda
    27 mins ago











  • sed is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.
    – RoVo
    26 mins ago















up vote
2
down vote

favorite












I am trying to get the last line of a file and then delete it:



Read the line:



sed -n '$p' file
# or
tail -n 1 file


Remove the line:



sed -i '$d' file


This works, but is there a way to do this with just a single command ?










share|improve this question























  • I'm not focused on sed. Any default tool will be fine.
    – RoVo
    29 mins ago











  • In what way does sed not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular way sed is not good enough. Or, do you want to perform both operations in one command?
    – Kusalananda
    27 mins ago











  • sed is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.
    – RoVo
    26 mins ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am trying to get the last line of a file and then delete it:



Read the line:



sed -n '$p' file
# or
tail -n 1 file


Remove the line:



sed -i '$d' file


This works, but is there a way to do this with just a single command ?










share|improve this question















I am trying to get the last line of a file and then delete it:



Read the line:



sed -n '$p' file
# or
tail -n 1 file


Remove the line:



sed -i '$d' file


This works, but is there a way to do this with just a single command ?







text-processing sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 26 mins ago

























asked 30 mins ago









RoVo

1,943213




1,943213











  • I'm not focused on sed. Any default tool will be fine.
    – RoVo
    29 mins ago











  • In what way does sed not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular way sed is not good enough. Or, do you want to perform both operations in one command?
    – Kusalananda
    27 mins ago











  • sed is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.
    – RoVo
    26 mins ago

















  • I'm not focused on sed. Any default tool will be fine.
    – RoVo
    29 mins ago











  • In what way does sed not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular way sed is not good enough. Or, do you want to perform both operations in one command?
    – Kusalananda
    27 mins ago











  • sed is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.
    – RoVo
    26 mins ago
















I'm not focused on sed. Any default tool will be fine.
– RoVo
29 mins ago





I'm not focused on sed. Any default tool will be fine.
– RoVo
29 mins ago













In what way does sed not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular way sed is not good enough. Or, do you want to perform both operations in one command?
– Kusalananda
27 mins ago





In what way does sed not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular way sed is not good enough. Or, do you want to perform both operations in one command?
– Kusalananda
27 mins ago













sed is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.
– RoVo
26 mins ago





sed is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.
– RoVo
26 mins ago











1 Answer
1






active

oldest

votes

















up vote
4
down vote













The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.



sed -i -e '$w /dev/stdout' -e 'd;' file


This would run the following sed script:



$
w /dev/stdout
d



This writes the last line to /dev/stdout and then deletes it. All other lines are written back into original file through the -i option.



The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w command (other than inserting a literal newline).



With ed:



ed -s file <<END_ED
p
d
w
END_ED


ed opens the file file and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed in this way may not be advisable on huge files.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474838%2fhow-to-pop-readremove-a-line-of-a-file%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote













    The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.



    sed -i -e '$w /dev/stdout' -e 'd;' file


    This would run the following sed script:



    $
    w /dev/stdout
    d



    This writes the last line to /dev/stdout and then deletes it. All other lines are written back into original file through the -i option.



    The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w command (other than inserting a literal newline).



    With ed:



    ed -s file <<END_ED
    p
    d
    w
    END_ED


    ed opens the file file and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed in this way may not be advisable on huge files.






    share|improve this answer


























      up vote
      4
      down vote













      The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.



      sed -i -e '$w /dev/stdout' -e 'd;' file


      This would run the following sed script:



      $
      w /dev/stdout
      d



      This writes the last line to /dev/stdout and then deletes it. All other lines are written back into original file through the -i option.



      The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w command (other than inserting a literal newline).



      With ed:



      ed -s file <<END_ED
      p
      d
      w
      END_ED


      ed opens the file file and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed in this way may not be advisable on huge files.






      share|improve this answer
























        up vote
        4
        down vote










        up vote
        4
        down vote









        The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.



        sed -i -e '$w /dev/stdout' -e 'd;' file


        This would run the following sed script:



        $
        w /dev/stdout
        d



        This writes the last line to /dev/stdout and then deletes it. All other lines are written back into original file through the -i option.



        The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w command (other than inserting a literal newline).



        With ed:



        ed -s file <<END_ED
        p
        d
        w
        END_ED


        ed opens the file file and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed in this way may not be advisable on huge files.






        share|improve this answer














        The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.



        sed -i -e '$w /dev/stdout' -e 'd;' file


        This would run the following sed script:



        $
        w /dev/stdout
        d



        This writes the last line to /dev/stdout and then deletes it. All other lines are written back into original file through the -i option.



        The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w command (other than inserting a literal newline).



        With ed:



        ed -s file <<END_ED
        p
        d
        w
        END_ED


        ed opens the file file and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed in this way may not be advisable on huge files.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 6 mins ago

























        answered 22 mins ago









        Kusalananda

        109k14211334




        109k14211334



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474838%2fhow-to-pop-readremove-a-line-of-a-file%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            List of Gilmore Girls characters

            What does second last employer means? [closed]

            One-line joke