以下将在本机 bash 中实现您的拆分逻辑——执行速度不是很快,但它可以在任何可以安装 bash 的地方工作,而无需运行第 3 方工具:
#!/bin/bash
prefix=${1:-"out."} # first optional argument: output file prefix
max_size=${2:-$(( 1024 * 1024 * 100 ))} # 2nd optional argument: size in bytes
cur_size=0 # running count: size of current chunk
file_num=1 # current numeric suffix; starting at 1
exec >"$prefix$file_num" # open first output file
while IFS= read -r -d $'\x02' piece; do # as long as there's new input...
printf '%s\x02' "$piece" # write it to our current output file
cur_size=$(( cur_size + ${#piece} + 1 )) # add its length to our counter
if (( cur_size > max_size )); then # if our counter is over our maximum size...
(( ++file_num )) # increment the file counter
exec >"$prefix$file_num" # open a new output file
cur_size=0 # and reset the output size counter
fi
done
if [[ $piece ]]; then # if the end of input had content without a \x02 after it...
printf '%s' "$piece" # ...write that trailing content to our output file.
fi
一个依赖dd
的版本(这里是 GNU 版本;可以更改为可移植的),但是对于大输入应该更快:
#!/bin/bash
prefix=${1:-"out."} # first optional argument: output file prefix
file_num=1 # current numeric suffix; starting at 1
exec >"$prefix$file_num" # open first output file
while true; do
dd bs=1M count=100 # tell GNU dd to copy 100MB from stdin to stdout
if IFS= read -r -d $'\x02' piece; then # read in bash to the next boundary
printf '%s\x02' "$piece" # write that segment to stdout
exec >"$prefix$((++file_num))" # re-open stdout to point to the next file
else
[[ $piece ]] && printf '%s' "$piece" # write what's left after the last boundary
break # and stop
fi
done
# if our last file is empty, delete it.
[[ -s $prefix$file_num ]] || rm -f -- "$prefix$file_num"