将文件拆分为 N 个名称相同但目标目录不同的片段

dan*_*one 6 text-processing split files

我想将 sourcefile.txt其中包含 10000 行(每天增加)分成 30 个相等的文件。我有调用prog1to 的目录prog30,我想将文件拆分为具有相同文件名的这些目录。例如/prog1/myfile.txt/prog2/myfile.txt/prog30/myfile.txt.

这是我的 bash 脚本,名为divide.shprog目录中运行

#!/bin/bash
programpath=/home/mywebsite/project/a1/
array=/prog1/
totalline=$(wc -l < ./sourcefile.txt)   
divide="$(( $totalline / 30 ))"   
split --lines=$divide $./prog1/myfile.txt    
exit 1
fi
Run Code Online (Sandbox Code Playgroud)

jth*_*ill 1

有趣的 Sed 版本:

lines=$(wc -l <sourcefile.txt)
perfile=$(( (lines+29)/30 ))     # see https://www.rfc-editor.org/rfc/rfc968.txt
last=0
sed -nf- sourcefile.txt <<EOD
$(while let $((last<lines)); do 
        mkdir -p prog$((last/perfile+1))
        echo $((last+1)),$((last+perfile)) w prog$((last/perfile+1))/myfile.txt
        : $((last+=perfile))
        done)
EOD
Run Code Online (Sandbox Code Playgroud)