Dixon MD
Dixon MD

Reputation: 188

Split a large file based on n th occurrence of a delimiter

Split a file into chunks based on the Nth occurrence of "//" in linux. Also don't remove the "//" at the end of the chunk files.

Sample input file:

ABC
BCDV
//
EFGF
HIJ
KLMDF
//
NOP
sdsd
sd sdvsd
sdsdsd dwe
//
er re er
DFer er
//
DFGHDF
//

If splitting with 2nd "//" output would be

First file

ABC
BCDV
//
EFGF
HIJ
KLMDF
//

Second file

NOP
sdsd
sd sdvsd
sdsdsd dwe
//
er re er
DFer er
//

Third file

DFGHDF
//

Upvotes: 0

Views: 190

Answers (1)

jas
jas

Reputation: 10865

Here's a solution using a multi-character RS (requires gnu awk):

$ awk -v n=2 'BEGIN { RS=ORS="//\n" } { print > ("xxx" int((NR-1)/n)) }' file

output:

$ cat xxx0
ABC
BCDV
//
EFGF
HIJ
KLMDF
//

$ cat xxx1
NOP
sdsd
sd sdvsd
sdsdsd dwe
//
er re er
DFer er
//

$ cat xxx2
DFGHDF
//

Upvotes: 1

Related Questions