简体   繁体   中英

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
//

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
//

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM