Negative arguments to head / tail

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











up vote
7
down vote

favorite
2












Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M lines from a file without luck.



The second most voted answer in this question recommends doing the following to get rid of the last line in a file:



head -n -1 foo.txt > temp.txt


However, when I try that in OSX & Zsh, I get:



head: illegal line count -- -1


Why is that? How can I remove the M last lines and the first N lines of a given file?










share|improve this question



















  • 1




    See POSIX head and tail not feature equivalent
    – Stéphane Chazelas
    Nov 20 '14 at 17:19






  • 1




    What is the output of head --version? What system are you using?
    – jofel
    Nov 20 '14 at 17:21










  • @jofel head --version returns an error actually.
    – Amelio Vazquez-Reina
    Nov 20 '14 at 17:33














up vote
7
down vote

favorite
2












Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M lines from a file without luck.



The second most voted answer in this question recommends doing the following to get rid of the last line in a file:



head -n -1 foo.txt > temp.txt


However, when I try that in OSX & Zsh, I get:



head: illegal line count -- -1


Why is that? How can I remove the M last lines and the first N lines of a given file?










share|improve this question



















  • 1




    See POSIX head and tail not feature equivalent
    – Stéphane Chazelas
    Nov 20 '14 at 17:19






  • 1




    What is the output of head --version? What system are you using?
    – jofel
    Nov 20 '14 at 17:21










  • @jofel head --version returns an error actually.
    – Amelio Vazquez-Reina
    Nov 20 '14 at 17:33












up vote
7
down vote

favorite
2









up vote
7
down vote

favorite
2






2





Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M lines from a file without luck.



The second most voted answer in this question recommends doing the following to get rid of the last line in a file:



head -n -1 foo.txt > temp.txt


However, when I try that in OSX & Zsh, I get:



head: illegal line count -- -1


Why is that? How can I remove the M last lines and the first N lines of a given file?










share|improve this question















Variants of this question have certainly been asked several times in different places, but I am trying to remove the last M lines from a file without luck.



The second most voted answer in this question recommends doing the following to get rid of the last line in a file:



head -n -1 foo.txt > temp.txt


However, when I try that in OSX & Zsh, I get:



head: illegal line count -- -1


Why is that? How can I remove the M last lines and the first N lines of a given file?







shell-script osx tail options head






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:40









Community

1




1










asked Nov 20 '14 at 17:17









Amelio Vazquez-Reina

12.2k52128228




12.2k52128228







  • 1




    See POSIX head and tail not feature equivalent
    – Stéphane Chazelas
    Nov 20 '14 at 17:19






  • 1




    What is the output of head --version? What system are you using?
    – jofel
    Nov 20 '14 at 17:21










  • @jofel head --version returns an error actually.
    – Amelio Vazquez-Reina
    Nov 20 '14 at 17:33












  • 1




    See POSIX head and tail not feature equivalent
    – Stéphane Chazelas
    Nov 20 '14 at 17:19






  • 1




    What is the output of head --version? What system are you using?
    – jofel
    Nov 20 '14 at 17:21










  • @jofel head --version returns an error actually.
    – Amelio Vazquez-Reina
    Nov 20 '14 at 17:33







1




1




See POSIX head and tail not feature equivalent
– Stéphane Chazelas
Nov 20 '14 at 17:19




See POSIX head and tail not feature equivalent
– Stéphane Chazelas
Nov 20 '14 at 17:19




1




1




What is the output of head --version? What system are you using?
– jofel
Nov 20 '14 at 17:21




What is the output of head --version? What system are you using?
– jofel
Nov 20 '14 at 17:21












@jofel head --version returns an error actually.
– Amelio Vazquez-Reina
Nov 20 '14 at 17:33




@jofel head --version returns an error actually.
– Amelio Vazquez-Reina
Nov 20 '14 at 17:33










3 Answers
3






active

oldest

votes

















up vote
14
down vote



accepted










You can remove the first 12 lines with:



tail -n +13


(That means print from the 13th line.)



Some implementations of head like GNU head support:



head -n -12


but that's not standard.



tail -r file | tail -n +12 | tail -r


would work on those systems that have tail -r (see also GNU tac) but is sub-optimal.



Where n is 1:



sed '$d' file


You can also do:



sed '$d' file | sed '$d'


to remove 2 lines, but that's not optimal.



You can do:



sed -ne :1 -e 'N;1,12b1' -e 'P;D'


But beware that won't work with large values of n with some sed implementations.



With awk:



awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'


To remove m lines from the beginning and n from the end:



awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'





share|improve this answer






















  • sed 'x;1d;$d' might be used to get the last two lines as well. And w/ a couple of N's after 1d might be used to get a little more than that, though at that point it offers no advantage over the N;P;D loop you already recommend.
    – mikeserv
    Nov 21 '14 at 0:46











  • Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work when m or n are 0. Otherwise it would be perfect.
    – Amelio Vazquez-Reina
    Nov 21 '14 at 21:13

















up vote
2
down vote













You can use the following way to remove first N lines and last M lines.



With N=5, M=7 and file test.txt:



sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt


The command prints all lines from N+1 to LastLine-M.



Another option is to use python:



python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt





share|improve this answer





























    up vote
    1
    down vote













    You can remove the last M (here M=100) lines from a file with:



    head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt





    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: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      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%2f169079%2fnegative-arguments-to-head-tail%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      14
      down vote



      accepted










      You can remove the first 12 lines with:



      tail -n +13


      (That means print from the 13th line.)



      Some implementations of head like GNU head support:



      head -n -12


      but that's not standard.



      tail -r file | tail -n +12 | tail -r


      would work on those systems that have tail -r (see also GNU tac) but is sub-optimal.



      Where n is 1:



      sed '$d' file


      You can also do:



      sed '$d' file | sed '$d'


      to remove 2 lines, but that's not optimal.



      You can do:



      sed -ne :1 -e 'N;1,12b1' -e 'P;D'


      But beware that won't work with large values of n with some sed implementations.



      With awk:



      awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'


      To remove m lines from the beginning and n from the end:



      awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'





      share|improve this answer






















      • sed 'x;1d;$d' might be used to get the last two lines as well. And w/ a couple of N's after 1d might be used to get a little more than that, though at that point it offers no advantage over the N;P;D loop you already recommend.
        – mikeserv
        Nov 21 '14 at 0:46











      • Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work when m or n are 0. Otherwise it would be perfect.
        – Amelio Vazquez-Reina
        Nov 21 '14 at 21:13














      up vote
      14
      down vote



      accepted










      You can remove the first 12 lines with:



      tail -n +13


      (That means print from the 13th line.)



      Some implementations of head like GNU head support:



      head -n -12


      but that's not standard.



      tail -r file | tail -n +12 | tail -r


      would work on those systems that have tail -r (see also GNU tac) but is sub-optimal.



      Where n is 1:



      sed '$d' file


      You can also do:



      sed '$d' file | sed '$d'


      to remove 2 lines, but that's not optimal.



      You can do:



      sed -ne :1 -e 'N;1,12b1' -e 'P;D'


      But beware that won't work with large values of n with some sed implementations.



      With awk:



      awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'


      To remove m lines from the beginning and n from the end:



      awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'





      share|improve this answer






















      • sed 'x;1d;$d' might be used to get the last two lines as well. And w/ a couple of N's after 1d might be used to get a little more than that, though at that point it offers no advantage over the N;P;D loop you already recommend.
        – mikeserv
        Nov 21 '14 at 0:46











      • Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work when m or n are 0. Otherwise it would be perfect.
        – Amelio Vazquez-Reina
        Nov 21 '14 at 21:13












      up vote
      14
      down vote



      accepted







      up vote
      14
      down vote



      accepted






      You can remove the first 12 lines with:



      tail -n +13


      (That means print from the 13th line.)



      Some implementations of head like GNU head support:



      head -n -12


      but that's not standard.



      tail -r file | tail -n +12 | tail -r


      would work on those systems that have tail -r (see also GNU tac) but is sub-optimal.



      Where n is 1:



      sed '$d' file


      You can also do:



      sed '$d' file | sed '$d'


      to remove 2 lines, but that's not optimal.



      You can do:



      sed -ne :1 -e 'N;1,12b1' -e 'P;D'


      But beware that won't work with large values of n with some sed implementations.



      With awk:



      awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'


      To remove m lines from the beginning and n from the end:



      awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'





      share|improve this answer














      You can remove the first 12 lines with:



      tail -n +13


      (That means print from the 13th line.)



      Some implementations of head like GNU head support:



      head -n -12


      but that's not standard.



      tail -r file | tail -n +12 | tail -r


      would work on those systems that have tail -r (see also GNU tac) but is sub-optimal.



      Where n is 1:



      sed '$d' file


      You can also do:



      sed '$d' file | sed '$d'


      to remove 2 lines, but that's not optimal.



      You can do:



      sed -ne :1 -e 'N;1,12b1' -e 'P;D'


      But beware that won't work with large values of n with some sed implementations.



      With awk:



      awk -v n=12 'NR>nprint line[NR%n];line[NR%n]=$0'


      To remove m lines from the beginning and n from the end:



      awk -v m=6 -v n=12 'NR<=mnext;NR>n+mprint line[NR%n];line[NR%n]=$0'






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jul 4 '15 at 15:10









      MichalH

      1,2081620




      1,2081620










      answered Nov 20 '14 at 17:27









      Stéphane Chazelas

      296k54559904




      296k54559904











      • sed 'x;1d;$d' might be used to get the last two lines as well. And w/ a couple of N's after 1d might be used to get a little more than that, though at that point it offers no advantage over the N;P;D loop you already recommend.
        – mikeserv
        Nov 21 '14 at 0:46











      • Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work when m or n are 0. Otherwise it would be perfect.
        – Amelio Vazquez-Reina
        Nov 21 '14 at 21:13
















      • sed 'x;1d;$d' might be used to get the last two lines as well. And w/ a couple of N's after 1d might be used to get a little more than that, though at that point it offers no advantage over the N;P;D loop you already recommend.
        – mikeserv
        Nov 21 '14 at 0:46











      • Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work when m or n are 0. Otherwise it would be perfect.
        – Amelio Vazquez-Reina
        Nov 21 '14 at 21:13















      sed 'x;1d;$d' might be used to get the last two lines as well. And w/ a couple of N's after 1d might be used to get a little more than that, though at that point it offers no advantage over the N;P;D loop you already recommend.
      – mikeserv
      Nov 21 '14 at 0:46





      sed 'x;1d;$d' might be used to get the last two lines as well. And w/ a couple of N's after 1d might be used to get a little more than that, though at that point it offers no advantage over the N;P;D loop you already recommend.
      – mikeserv
      Nov 21 '14 at 0:46













      Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work when m or n are 0. Otherwise it would be perfect.
      – Amelio Vazquez-Reina
      Nov 21 '14 at 21:13




      Thank you -- The last command is extremely helpful. A summer small caveat: it doesn't work when m or n are 0. Otherwise it would be perfect.
      – Amelio Vazquez-Reina
      Nov 21 '14 at 21:13












      up vote
      2
      down vote













      You can use the following way to remove first N lines and last M lines.



      With N=5, M=7 and file test.txt:



      sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt


      The command prints all lines from N+1 to LastLine-M.



      Another option is to use python:



      python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt





      share|improve this answer


























        up vote
        2
        down vote













        You can use the following way to remove first N lines and last M lines.



        With N=5, M=7 and file test.txt:



        sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt


        The command prints all lines from N+1 to LastLine-M.



        Another option is to use python:



        python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt





        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          You can use the following way to remove first N lines and last M lines.



          With N=5, M=7 and file test.txt:



          sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt


          The command prints all lines from N+1 to LastLine-M.



          Another option is to use python:



          python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt





          share|improve this answer














          You can use the following way to remove first N lines and last M lines.



          With N=5, M=7 and file test.txt:



          sed -n -e "6,$(($(wc -l < test.txt) - 7))p" test.txt


          The command prints all lines from N+1 to LastLine-M.



          Another option is to use python:



          python -c 'import sys;print "".join(sys.stdin.readlines()[5:-7]),' < test.txt






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '14 at 17:52

























          answered Nov 20 '14 at 17:45









          jofel

          20k34780




          20k34780




















              up vote
              1
              down vote













              You can remove the last M (here M=100) lines from a file with:



              head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt





              share|improve this answer
























                up vote
                1
                down vote













                You can remove the last M (here M=100) lines from a file with:



                head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can remove the last M (here M=100) lines from a file with:



                  head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt





                  share|improve this answer












                  You can remove the last M (here M=100) lines from a file with:



                  head -$(($(wc -l < foo.txt) - 100)) foo.txt > temp.txt






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 29 at 8:15









                  make

                  111




                  111



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f169079%2fnegative-arguments-to-head-tail%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      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