0

I want to start with a file that looks like this:

1
2
3
4
5
1
2
3
4
5
1
2
3
4
5

Return only the last three of every group of five like so:

3
4
5
3
4
5
3
4
5

I've managed to get the first 2 using a code like this:

awk -vn=2 -vm=5 'NR<=i{next}; (NR-i)%m==1{c=1}; c++<=n

but I'm having trouble doing the same for the last grouping.

2

These things are often easier using modulo arithmetic e.g.

awk '(NR-2)%5 && (NR-1)%5' file
3
4
5
3
4
5
3
4
5

Alternatively, using GNU sed's n~m ("n skip m") notation:

sed '1~5d;2~5d' file
  • 1
    or awk '(NR-1)%5>1' ... – Sundeep Mar 15 '18 at 4:16
  • 1
    Another way sed '1~5{N;d}'. – MiniMax Mar 15 '18 at 9:57

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