Question

I want to skip two lines and delete two lines till the end of file

if my file contains

111
222
333
444
555
666
777
888
999
1010
1111

I want to modify it to look like

111
222
555
666
999
1010
Was it helpful?

Solution

Using awk

awk 'NR%4==1 || NR%4==2' file
111
222
555
666
999
1010

NR%4==1 prints line 1,5,9 etc
NR%4==2 prints line 2,6,10 etc
So it gives line 1,2 5,6 9,19 etc


Other version:

awk 'NR%4~/^[12]$/' #JS웃
awk '(NR-1)%4<2'    #Ed Morton

OTHER TIPS

Using sed:

sed -n 'p;n;p;n;n' inputfile

For your given input, it'd produce:

111
222
555
666
999
1010

Using GNU sed:

sed -n '1~4p;2~4p' inputfile

For fun, one could also use perl:

perl -ne 'print if grep($_==$.%4,(1,2))' inputfile

This might work for you (GNU sed):

sed 'n;n;$!N;d' file

A vim only solution:

:%norm Jj2dd Aaaaa

then

:%s/aaaa /\r/g

You need to split the first line manually(have n), rest everything is in place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top