Search code examples
sh

How to skip a line every two lines starting by skipping the first line?


Here's my code : ls -lt | sed -n 'p;n'

That code makes me skip from a line to another when listing file names but doesn't start by skipping the first one, how to make that happen?

Here's an exemple without my code to skip to make it clear:

enter image description here

And here's an exemple of when I use the skip code: enter image description here


Solution

  • You have to invert your sed command: it should be n;p instead of p;n:

    Your code:

    for x in {1..20}; do echo $x ; done | sed -n 'p;n'
    
    1
    3
    5
    7
    9
    11
    13
    15
    17
    19
    

    The version with sed inverted:

    for x in {1..20}; do echo $x ; done | sed -n 'n;p'
    

    Output:

    2
    4
    6
    8
    10
    12
    14
    16
    18
    20