I am trying to split a big text files after n number of empty lines. The text file contains exactly one empty line as data separator. Like below:
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem
Lorem
...
I have tried to use csplit
csplit data.txt /^$/ {3}
My expectation is that after 3 empty lines (not consecutive, but after cursor processes 3 empty lines) it split file and continue to do so. But it actualy splits file in each empty line.
My expected files: xx00
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
Lorem ipsum
xx01
Lorem ipsum
Lorem ipsum
Lorem
Lorem
Any suggestion?
CodePudding user response:
With awk
(tested with GNU and BSD awk
):
awk -v max=3 '{print > sprintf("xxd", int(n/max))} /^$/ {n = 1}' file
CodePudding user response:
This awk
should also work with an empty RS
:
awk -v n=3 -v RS= '{ORS=RT; print > sprintf("xxd", int((NR-1)/n))}' file
CodePudding user response:
awk is good for this.
Split every n
empty lines, naming files with:
No leading zeroes:
awk -v n=3 '
$0 == "" { c}
c <= n {print > "xx"f}
c==n {c=0; f}'
width
minimum width/zeroes:
awk -v n=3 -v width=2 '
$0 == "" { c}
c <= n {print > "xx"f}
c==n {c=0; f; f = sprintf("%0*d",width,f)}'
To remove the trailing empty line in each file, just change c <= n
to c < n
.