3
$ sed '/b/,~8 d' a-i.txt

I know what it does. But, they say "a multiple of 8" for ~8. Why is it expressed as "a multiple of 8"?

1 Answer 1

5

/b/,~8 matches lines starting with a line containing b and continuing until a line is reached whose line number is a multiple of 8

From man sed:

addr1,~N
Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.

Examples

This prints any line which contains 5 and all following lines up to and including the next line whose line number is a multiple of 8:

$ seq 14 | sed -n '/5/,~8 p'
5
6
7
8

This prints from a line containing 14 to the next line whose line number is a multiple of 8:

$ seq 22 | sed -n '/14/,~8 p'
14
15
16

This deletes all lines starting with a line matches 5 and ending with the next line whose line number is a multiple of 8:

$ seq 10 | sed '/5/,~8 d'
1
2
3
4
9
10

Similar but different

This prints the fourth line and every eigtht line thereafter:

$ seq 22 | sed -n '4~8 p'
4
12
20

This prints a line matching 4 and the next three lines which follow:

$ seq 13 | sed -n '/4/,+3 p'
4
5
6
7
4
  • What do you mean with "a multiple of 8"?? It is just line 8, isn't it??
    – Smile
    Dec 30, 2017 at 8:15
  • 1
    @Smile Or line 16, or line 24, etc. I just added an example to show it printing up to line 16.
    – John1024
    Dec 30, 2017 at 8:18
  • How do you write the examples and results like that? Is it just working if I click the "code" and write the $ sed 10 ..., then the result is automatic?? I'm just curious. You seems to be fast for editting your post.
    – Smile
    Dec 30, 2017 at 8:20
  • 1
    @Smile I am copying-and-pasting from a terminal. So I type, say, seq 22 | sed -n '4~8 p' into a terminal, press enter, and copy-and-paste the results to here. Once I get here, I need to indent it by four spaces. I do that by highlighting it and typing Ctrl-K.
    – John1024
    Dec 30, 2017 at 8:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .