Calculate split percentage as part of the split command line
The split command is one gem of a Giant Dork tool, but it expects to be given the number of lines to split a file along. It's easy enough to fire up a calculator to do this, but sometimes a programmatic method is desirable. Here's a bashism that'll do it:
split -l $(echo $(( $(cat sourcefile | wc -l) / 3))) sourcefile sourcefile.
Assuming a file called sourcefile has 270 lines in it, the above will produce three smaller files of 90 lines each:
ak@pork:~$ wc -l sourcefile.*
90 sourcefile.aa
90 sourcefile.ab
90 sourcefile.ac
270 total
If the number of lines doesn't devide up neatly (let's say sourcefile contains 271 lines), split will produce an extra file with the difference:
ak@pork:~$ wc -l sourcefile.*
90 sourcefile.aa
90 sourcefile.ab
90 sourcefile.ac
1 sourcefile.ad
271 total
I'm personally partial to numeric suffixes:
ak@pork:~$ split -l $(echo $(( $(cat sourcefile | wc -l) / 3))) -da3 sourcefile sourcefile.
ak@pork:~$ ls -1
sourcefile
sourcefile.000
sourcefile.001
sourcefile.002
sourcefile.003
1 Comment
1. Nerd Trails replies at 28th March 2014, 9:48 am :
Hey Alain,
I love that you’re proud of being a dork! I’ve always been a proud nerd. Thanks for the quick tip. I was looking around for how to split by a percentage.
Your new visitor,
Deborah
Leave a comment