Home > Software design >  How to skip a line every two lines starting by skipping the first line?
How to skip a line every two lines starting by skipping the first line?

Time:02-10

Hey guys I need some help over here

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

CodePudding user response:

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

CodePudding user response:

You can use sed's ~ operator: first~step

$ seq 1 10 | sed -n '1~2p'
1
3
5
7
9
$ seq 1 10 | sed -n '2~2p'
2
4
6
8
10
  • Related