Lets assume I have the following file text.txt
:
@something
@somethingelse
@anotherthing
1
2
2
3
3
3
4
4
4
5
5
6
7
7
8
9
9
9
10
11
11
11
14
15
I want to split this into multiple files by every 5th data row, but if the number of the next row is identical it should still end up in the same file. Header should be in every file, but that could also be ignored and reintroduced later.
This means something like this:
text.txt.1
@something
@somethingelse
@anotherthing
1
2
2
3
3
3
text.txt.2
@something
@somethingelse
@anotherthing
4
4
4
5
5
text.txt.3
@something
@somethingelse
@anotherthing
6
7
7
8
9
9
9
text.txt.4
@something
@somethingelse
@anotherthing
10
11
11
11
14
text.txt.5
@something
@somethingelse
@anotherthing
15
So I was thinking about something like this:
awk 'NR%5==1 && $1!=prev{i++;prev=$1}{print > FILENAME"."i}' test.txt
Both statements work by itself but not together.. is that possible using awk?