How to split the file in Bourne Shell with the number of split files in the filename -
i need split larger file. use following command split.
split -l 100 ./myfile.csv myfile this creates file myfileaa, myfileab, myfileac.....
but want different naming format. example total number of files getting split 4, want filenames this
myfile1_4 myfile2_4 myfile3_4 myfile4_4 can please how filenames this? thanks.
one option implement split's functionality yourself.
this implementation assumes /bin/sh posix sh, not actual 1970s-era bourne.
#!/bin/sh input_file="$1" output_prefix="$2" lines_per_file=${3:-100} total_count=$(wc -l <"$input_file") total_splits=$(( total_count / lines_per_file )) if [ "$input_file" ]; exec <"$input_file" fi # handle remainder lines_handled=$(( total_splits * lines_per_file )) if [ "$lines_handled" -lt "$total_count" ]; total_splits=$(( total_splits + 1 )) fi current_split=1 lines_left=$lines_per_file exec >"${output_prefix}${current_split}_${total_splits}" while read -r; printf '%s\n' "$reply" lines_left=$(( lines_left - 1 )) if [ "$lines_left" -le 0 ]; current_split=$(( current_split + 1 )) exec >"${input_file}_${current_split}_${total_splits}" lines_left=$lines_per_file fi done you can save above numeric-split, , run:
./numeric-split myfile.csv myfile 100 if you're running on solaris 10 (the modern-ish operating system ship actual bourne shell), please edit shebang point #!/bin/ksh instead; modern ksh superset of posix sh, whereas bourne shell predates standard 2 decades.
Comments
Post a Comment