Rename files generated by split to offset numeric ranges
The "split" program is fantastic, but offers no option (that I know of) to offset the start of a numeric range. For instance, let's say I split a file into 30 with:
split -l $(echo $(( $(cat sourcefile | wc -l) / 30))) -da3 sourcefile sourcefile.
Split would produce:
ls -1
sourcefile.000
sourcefile.001
sourcefile.002
sourcefile.003
sourcefile.004
sourcefile.005
sourcefile.006
sourcefile.007
sourcefile.008
sourcefile.009
sourcefile.010
sourcefile.011
sourcefile.012
sourcefile.013
sourcefile.014
sourcefile.015
sourcefile.016
sourcefile.017
sourcefile.018
sourcefile.019
sourcefile.020
sourcefile.021
sourcefile.022
sourcefile.023
sourcefile.024
sourcefile.025
sourcefile.026
sourcefile.027
sourcefile.028
sourcefile.029
Thanks, but what if I want file names to be named .001 through .030 instead? Here's one way:
filepattern='sourcefile.*'
for onp in $(seq -w 000 029 | tac); do
on=$(( 10#$onp ))
nn=$(( $on + 1 ))
nnp=$(echo $nn | sed '/^[1-9]$/s/^/00/;/^[1-9][0-9]$/s/^/0/;')
rename -vf "s/\.$onp/\.$nnp/" $filepattern
done
sourcefile.029 renamed as sourcefile.030
sourcefile.028 renamed as sourcefile.029
sourcefile.027 renamed as sourcefile.028
sourcefile.026 renamed as sourcefile.027
sourcefile.025 renamed as sourcefile.026
sourcefile.024 renamed as sourcefile.025
sourcefile.023 renamed as sourcefile.024
sourcefile.022 renamed as sourcefile.023
sourcefile.021 renamed as sourcefile.022
sourcefile.020 renamed as sourcefile.021
sourcefile.019 renamed as sourcefile.020
sourcefile.018 renamed as sourcefile.019
sourcefile.017 renamed as sourcefile.018
sourcefile.016 renamed as sourcefile.017
sourcefile.015 renamed as sourcefile.016
sourcefile.014 renamed as sourcefile.015
sourcefile.013 renamed as sourcefile.014
sourcefile.012 renamed as sourcefile.013
sourcefile.011 renamed as sourcefile.012
sourcefile.010 renamed as sourcefile.011
sourcefile.009 renamed as sourcefile.010
sourcefile.008 renamed as sourcefile.009
sourcefile.007 renamed as sourcefile.008
sourcefile.006 renamed as sourcefile.007
sourcefile.005 renamed as sourcefile.006
sourcefile.004 renamed as sourcefile.005
sourcefile.003 renamed as sourcefile.004
sourcefile.002 renamed as sourcefile.003
sourcefile.001 renamed as sourcefile.002
sourcefile.000 renamed as sourcefile.001
If you know of a more elegant way, please leave a comment!
Leave a comment