How to move the last line in a file? [duplicate]

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











up vote
2
down vote

favorite













This question already has an answer here:



  • Need to move the last line of the file to second line of the same file

    4 answers



Example of a file



line 1
line 2
line 3
line 4


line 4 should be after line 1



line 1
line 4
line 2
line 3


Important: It could be more than 4 lines somehow the script has to understand that it has to read the last line, not line 4.










share|improve this question















marked as duplicate by don_crissti, Kusalananda, roaima, Sundeep, Jeff Schaller Sep 8 at 11:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Not very effective but it works with GNU sed: sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
    – Cyrus
    Sep 8 at 9:54










  • Bash isn’t a text editor
    – Jeff Schaller
    Sep 8 at 9:58














up vote
2
down vote

favorite













This question already has an answer here:



  • Need to move the last line of the file to second line of the same file

    4 answers



Example of a file



line 1
line 2
line 3
line 4


line 4 should be after line 1



line 1
line 4
line 2
line 3


Important: It could be more than 4 lines somehow the script has to understand that it has to read the last line, not line 4.










share|improve this question















marked as duplicate by don_crissti, Kusalananda, roaima, Sundeep, Jeff Schaller Sep 8 at 11:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Not very effective but it works with GNU sed: sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
    – Cyrus
    Sep 8 at 9:54










  • Bash isn’t a text editor
    – Jeff Schaller
    Sep 8 at 9:58












up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:



  • Need to move the last line of the file to second line of the same file

    4 answers



Example of a file



line 1
line 2
line 3
line 4


line 4 should be after line 1



line 1
line 4
line 2
line 3


Important: It could be more than 4 lines somehow the script has to understand that it has to read the last line, not line 4.










share|improve this question
















This question already has an answer here:



  • Need to move the last line of the file to second line of the same file

    4 answers



Example of a file



line 1
line 2
line 3
line 4


line 4 should be after line 1



line 1
line 4
line 2
line 3


Important: It could be more than 4 lines somehow the script has to understand that it has to read the last line, not line 4.





This question already has an answer here:



  • Need to move the last line of the file to second line of the same file

    4 answers







text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 8 at 10:13









don_crissti

47.4k15126155




47.4k15126155










asked Sep 8 at 6:12









John Doe

263




263




marked as duplicate by don_crissti, Kusalananda, roaima, Sundeep, Jeff Schaller Sep 8 at 11:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by don_crissti, Kusalananda, roaima, Sundeep, Jeff Schaller Sep 8 at 11:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Not very effective but it works with GNU sed: sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
    – Cyrus
    Sep 8 at 9:54










  • Bash isn’t a text editor
    – Jeff Schaller
    Sep 8 at 9:58
















  • Not very effective but it works with GNU sed: sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
    – Cyrus
    Sep 8 at 9:54










  • Bash isn’t a text editor
    – Jeff Schaller
    Sep 8 at 9:58















Not very effective but it works with GNU sed: sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
– Cyrus
Sep 8 at 9:54




Not very effective but it works with GNU sed: sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
– Cyrus
Sep 8 at 9:54












Bash isn’t a text editor
– Jeff Schaller
Sep 8 at 9:58




Bash isn’t a text editor
– Jeff Schaller
Sep 8 at 9:58










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










An alternative, slower, longer version.



#!/bin/sh

if [ ! -f "$1" ]
then
printf "No input filen"
exit
fi

FIRST=`head -n 1 "$1"`
LAST=`tail -n 1 "$1"`
MID=`sed -n '$d; 2,$p' "$1"`
printf "$FIRSTn$LASTn$MIDn"

exit


Slower because using head, tail and sed to access the file each time. On a large file (or many files) this could be very noticeable.






share|improve this answer






















  • The sed pipeline could be shortened into sed -n '$d; 2,$p'
    – Kusalananda
    Sep 8 at 8:59










  • @Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
    – Tigger
    Sep 8 at 12:11

















up vote
2
down vote













$ printf '4m1n,pn' | ed -s file
line 1
line 4
line 2
line 3


The short ed script



4m1
,p


will move line four to after line one, and then display the contents of the editing buffer in the terminal.



To save the result to a new file, you may use a redirection like



printf '4m1n,pn' | ed -s file >newfile


or you may tell ed to save the file with



printf '4m1nw newfilen' | ed -s file


The command w newfile would save the edited buffer into the named file. The command w with no filename would save the file back to the original filename.



Since the line editor ed reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).




To move the last line, change 4 to $ in the commands above.






share|improve this answer





























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    An alternative, slower, longer version.



    #!/bin/sh

    if [ ! -f "$1" ]
    then
    printf "No input filen"
    exit
    fi

    FIRST=`head -n 1 "$1"`
    LAST=`tail -n 1 "$1"`
    MID=`sed -n '$d; 2,$p' "$1"`
    printf "$FIRSTn$LASTn$MIDn"

    exit


    Slower because using head, tail and sed to access the file each time. On a large file (or many files) this could be very noticeable.






    share|improve this answer






















    • The sed pipeline could be shortened into sed -n '$d; 2,$p'
      – Kusalananda
      Sep 8 at 8:59










    • @Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
      – Tigger
      Sep 8 at 12:11














    up vote
    2
    down vote



    accepted










    An alternative, slower, longer version.



    #!/bin/sh

    if [ ! -f "$1" ]
    then
    printf "No input filen"
    exit
    fi

    FIRST=`head -n 1 "$1"`
    LAST=`tail -n 1 "$1"`
    MID=`sed -n '$d; 2,$p' "$1"`
    printf "$FIRSTn$LASTn$MIDn"

    exit


    Slower because using head, tail and sed to access the file each time. On a large file (or many files) this could be very noticeable.






    share|improve this answer






















    • The sed pipeline could be shortened into sed -n '$d; 2,$p'
      – Kusalananda
      Sep 8 at 8:59










    • @Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
      – Tigger
      Sep 8 at 12:11












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    An alternative, slower, longer version.



    #!/bin/sh

    if [ ! -f "$1" ]
    then
    printf "No input filen"
    exit
    fi

    FIRST=`head -n 1 "$1"`
    LAST=`tail -n 1 "$1"`
    MID=`sed -n '$d; 2,$p' "$1"`
    printf "$FIRSTn$LASTn$MIDn"

    exit


    Slower because using head, tail and sed to access the file each time. On a large file (or many files) this could be very noticeable.






    share|improve this answer














    An alternative, slower, longer version.



    #!/bin/sh

    if [ ! -f "$1" ]
    then
    printf "No input filen"
    exit
    fi

    FIRST=`head -n 1 "$1"`
    LAST=`tail -n 1 "$1"`
    MID=`sed -n '$d; 2,$p' "$1"`
    printf "$FIRSTn$LASTn$MIDn"

    exit


    Slower because using head, tail and sed to access the file each time. On a large file (or many files) this could be very noticeable.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 8 at 12:08

























    answered Sep 8 at 8:23









    Tigger

    1,981812




    1,981812











    • The sed pipeline could be shortened into sed -n '$d; 2,$p'
      – Kusalananda
      Sep 8 at 8:59










    • @Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
      – Tigger
      Sep 8 at 12:11
















    • The sed pipeline could be shortened into sed -n '$d; 2,$p'
      – Kusalananda
      Sep 8 at 8:59










    • @Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
      – Tigger
      Sep 8 at 12:11















    The sed pipeline could be shortened into sed -n '$d; 2,$p'
    – Kusalananda
    Sep 8 at 8:59




    The sed pipeline could be shortened into sed -n '$d; 2,$p'
    – Kusalananda
    Sep 8 at 8:59












    @Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
    – Tigger
    Sep 8 at 12:11




    @Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
    – Tigger
    Sep 8 at 12:11












    up vote
    2
    down vote













    $ printf '4m1n,pn' | ed -s file
    line 1
    line 4
    line 2
    line 3


    The short ed script



    4m1
    ,p


    will move line four to after line one, and then display the contents of the editing buffer in the terminal.



    To save the result to a new file, you may use a redirection like



    printf '4m1n,pn' | ed -s file >newfile


    or you may tell ed to save the file with



    printf '4m1nw newfilen' | ed -s file


    The command w newfile would save the edited buffer into the named file. The command w with no filename would save the file back to the original filename.



    Since the line editor ed reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).




    To move the last line, change 4 to $ in the commands above.






    share|improve this answer


























      up vote
      2
      down vote













      $ printf '4m1n,pn' | ed -s file
      line 1
      line 4
      line 2
      line 3


      The short ed script



      4m1
      ,p


      will move line four to after line one, and then display the contents of the editing buffer in the terminal.



      To save the result to a new file, you may use a redirection like



      printf '4m1n,pn' | ed -s file >newfile


      or you may tell ed to save the file with



      printf '4m1nw newfilen' | ed -s file


      The command w newfile would save the edited buffer into the named file. The command w with no filename would save the file back to the original filename.



      Since the line editor ed reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).




      To move the last line, change 4 to $ in the commands above.






      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        $ printf '4m1n,pn' | ed -s file
        line 1
        line 4
        line 2
        line 3


        The short ed script



        4m1
        ,p


        will move line four to after line one, and then display the contents of the editing buffer in the terminal.



        To save the result to a new file, you may use a redirection like



        printf '4m1n,pn' | ed -s file >newfile


        or you may tell ed to save the file with



        printf '4m1nw newfilen' | ed -s file


        The command w newfile would save the edited buffer into the named file. The command w with no filename would save the file back to the original filename.



        Since the line editor ed reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).




        To move the last line, change 4 to $ in the commands above.






        share|improve this answer














        $ printf '4m1n,pn' | ed -s file
        line 1
        line 4
        line 2
        line 3


        The short ed script



        4m1
        ,p


        will move line four to after line one, and then display the contents of the editing buffer in the terminal.



        To save the result to a new file, you may use a redirection like



        printf '4m1n,pn' | ed -s file >newfile


        or you may tell ed to save the file with



        printf '4m1nw newfilen' | ed -s file


        The command w newfile would save the edited buffer into the named file. The command w with no filename would save the file back to the original filename.



        Since the line editor ed reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).




        To move the last line, change 4 to $ in the commands above.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 8 at 8:07

























        answered Sep 8 at 8:02









        Kusalananda

        107k14209331




        107k14209331












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay