Remove even numbered lines of text file [duplicate]












3















This question already has an answer here:




  • Print odd-numbered lines, print even-numbered lines

    7 answers




I have a text file on a Linux system, and would like to remove every second line, which is "even" numbered. Example: I would like to remove the second line of the file, the fourth, sixth, eighth line, and so on, until you complete filtering of the file completely.



file.txt



86850343
88065952
89381968
89536251
89714939
89826424
90124775
90672109
91408453
92438737


I would like this output:



86850343
89381968
89714939
90124775
91408453









share|improve this question















marked as duplicate by don_crissti, Sparhawk, terdon awk
Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 2:02


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.




















    3















    This question already has an answer here:




    • Print odd-numbered lines, print even-numbered lines

      7 answers




    I have a text file on a Linux system, and would like to remove every second line, which is "even" numbered. Example: I would like to remove the second line of the file, the fourth, sixth, eighth line, and so on, until you complete filtering of the file completely.



    file.txt



    86850343
    88065952
    89381968
    89536251
    89714939
    89826424
    90124775
    90672109
    91408453
    92438737


    I would like this output:



    86850343
    89381968
    89714939
    90124775
    91408453









    share|improve this question















    marked as duplicate by don_crissti, Sparhawk, terdon awk
    Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Jan 2 at 2:02


    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.


















      3












      3








      3


      1






      This question already has an answer here:




      • Print odd-numbered lines, print even-numbered lines

        7 answers




      I have a text file on a Linux system, and would like to remove every second line, which is "even" numbered. Example: I would like to remove the second line of the file, the fourth, sixth, eighth line, and so on, until you complete filtering of the file completely.



      file.txt



      86850343
      88065952
      89381968
      89536251
      89714939
      89826424
      90124775
      90672109
      91408453
      92438737


      I would like this output:



      86850343
      89381968
      89714939
      90124775
      91408453









      share|improve this question
















      This question already has an answer here:




      • Print odd-numbered lines, print even-numbered lines

        7 answers




      I have a text file on a Linux system, and would like to remove every second line, which is "even" numbered. Example: I would like to remove the second line of the file, the fourth, sixth, eighth line, and so on, until you complete filtering of the file completely.



      file.txt



      86850343
      88065952
      89381968
      89536251
      89714939
      89826424
      90124775
      90672109
      91408453
      92438737


      I would like this output:



      86850343
      89381968
      89714939
      90124775
      91408453




      This question already has an answer here:




      • Print odd-numbered lines, print even-numbered lines

        7 answers








      text-processing awk sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 7:52









      Rui F Ribeiro

      39.4k1479131




      39.4k1479131










      asked Jan 2 at 0:58









      logvcalogvca

      313




      313




      marked as duplicate by don_crissti, Sparhawk, terdon awk
      Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 2 at 2:02


      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, Sparhawk, terdon awk
      Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 2 at 2:02


      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.
























          2 Answers
          2






          active

          oldest

          votes


















          3














          With GNU Sed's n-skip-m notation:



          $ sed '2~2d' file.txt
          86850343
          89381968
          89714939
          90124775
          91408453





          share|improve this answer























          • Thanks! Works fine!!!
            – logvca
            Jan 2 at 1:03



















          2














          You can't really beat sed for this sort of thing, but here are some other options:



          $ awk 'NR%2!=0' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne '$.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'print if $.%2' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'not $.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453





          share|improve this answer



















          • 2




            Heh, the first 3 of those give incorrect output based on the question; the fourth gives the right result :-)
            – Stephen Harris
            Jan 2 at 1:30






          • 1




            @StephenHarris eeeek! Completely fumbled that one. Thanks for the heads up, fixed now.
            – terdon
            Jan 2 at 1:41


















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          With GNU Sed's n-skip-m notation:



          $ sed '2~2d' file.txt
          86850343
          89381968
          89714939
          90124775
          91408453





          share|improve this answer























          • Thanks! Works fine!!!
            – logvca
            Jan 2 at 1:03
















          3














          With GNU Sed's n-skip-m notation:



          $ sed '2~2d' file.txt
          86850343
          89381968
          89714939
          90124775
          91408453





          share|improve this answer























          • Thanks! Works fine!!!
            – logvca
            Jan 2 at 1:03














          3












          3








          3






          With GNU Sed's n-skip-m notation:



          $ sed '2~2d' file.txt
          86850343
          89381968
          89714939
          90124775
          91408453





          share|improve this answer














          With GNU Sed's n-skip-m notation:



          $ sed '2~2d' file.txt
          86850343
          89381968
          89714939
          90124775
          91408453






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 1:07

























          answered Jan 2 at 1:00









          steeldriversteeldriver

          34.8k35184




          34.8k35184












          • Thanks! Works fine!!!
            – logvca
            Jan 2 at 1:03


















          • Thanks! Works fine!!!
            – logvca
            Jan 2 at 1:03
















          Thanks! Works fine!!!
          – logvca
          Jan 2 at 1:03




          Thanks! Works fine!!!
          – logvca
          Jan 2 at 1:03













          2














          You can't really beat sed for this sort of thing, but here are some other options:



          $ awk 'NR%2!=0' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne '$.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'print if $.%2' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'not $.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453





          share|improve this answer



















          • 2




            Heh, the first 3 of those give incorrect output based on the question; the fourth gives the right result :-)
            – Stephen Harris
            Jan 2 at 1:30






          • 1




            @StephenHarris eeeek! Completely fumbled that one. Thanks for the heads up, fixed now.
            – terdon
            Jan 2 at 1:41
















          2














          You can't really beat sed for this sort of thing, but here are some other options:



          $ awk 'NR%2!=0' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne '$.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'print if $.%2' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'not $.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453





          share|improve this answer



















          • 2




            Heh, the first 3 of those give incorrect output based on the question; the fourth gives the right result :-)
            – Stephen Harris
            Jan 2 at 1:30






          • 1




            @StephenHarris eeeek! Completely fumbled that one. Thanks for the heads up, fixed now.
            – terdon
            Jan 2 at 1:41














          2












          2








          2






          You can't really beat sed for this sort of thing, but here are some other options:



          $ awk 'NR%2!=0' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne '$.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'print if $.%2' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'not $.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453





          share|improve this answer














          You can't really beat sed for this sort of thing, but here are some other options:



          $ awk 'NR%2!=0' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne '$.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'print if $.%2' file
          86850343
          89381968
          89714939
          90124775
          91408453

          $ perl -ne 'not $.%2 && print' file
          86850343
          89381968
          89714939
          90124775
          91408453






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 1:40

























          answered Jan 2 at 1:03









          terdonterdon

          129k32253428




          129k32253428








          • 2




            Heh, the first 3 of those give incorrect output based on the question; the fourth gives the right result :-)
            – Stephen Harris
            Jan 2 at 1:30






          • 1




            @StephenHarris eeeek! Completely fumbled that one. Thanks for the heads up, fixed now.
            – terdon
            Jan 2 at 1:41














          • 2




            Heh, the first 3 of those give incorrect output based on the question; the fourth gives the right result :-)
            – Stephen Harris
            Jan 2 at 1:30






          • 1




            @StephenHarris eeeek! Completely fumbled that one. Thanks for the heads up, fixed now.
            – terdon
            Jan 2 at 1:41








          2




          2




          Heh, the first 3 of those give incorrect output based on the question; the fourth gives the right result :-)
          – Stephen Harris
          Jan 2 at 1:30




          Heh, the first 3 of those give incorrect output based on the question; the fourth gives the right result :-)
          – Stephen Harris
          Jan 2 at 1:30




          1




          1




          @StephenHarris eeeek! Completely fumbled that one. Thanks for the heads up, fixed now.
          – terdon
          Jan 2 at 1:41




          @StephenHarris eeeek! Completely fumbled that one. Thanks for the heads up, fixed now.
          – terdon
          Jan 2 at 1:41



          Popular posts from this blog

          Human spaceflight

          Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

          張江高科駅