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:
1 | 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:
1 2 3 4 5 | 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:
1 2 3 4 5 6 | 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:
1 2 3 4 5 6 7 | 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 |
Leave a comment