1

This question already has an answer here:

I want to be able print line 1,2,3,4,7 by skipping Lines 5,6,8,9 and repeat the pattern till the end of file.

Sample INPUT:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
Line 13
Line 14
Line 15
Line 16
Line 17
Line 18

Desired Output:

Line 1
Line 2
Line 3
Line 4
Line 7
Line 10
Line 11
Line 12
Line 13
Line 16

Using Solaris 10, KSH. Would love an nawk or sed solution

marked as duplicate by Sundeep, Archemar, user34720, meuh, Jeff Schaller Feb 28 '18 at 11:14

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.

  • from the duplicate, the last answer would be fastest - awk 'BEGIN{ a[1] a[2] a[3] a[4] a[7] }; NR % 9 in a' – Sundeep Feb 28 '18 at 5:09
4

If I understand correctly, you are looking for a pattern that repeats every nine lines. Try:

$ awk '{r=NR % 9} r>0 && (r<=4 || r==7)' input
Line 1
Line 2
Line 3
Line 4
Line 7
Line 10
Line 11
Line 12
Line 13
Line 16

How it works

First, we use r=NR % 9 to set r to the line number modulo 9. Next, we select which of those nine lines we want to keep: r>0 && (r<=4 || r==7).

Not the answer you're looking for? Browse other questions tagged or ask your own question.