每隔n行拆分批量文本文件 [英] Splitting bulk text file every n line
问题描述
我有一个包含多个文本文件的文件夹。我试图拆分所有文本文件在10000行每个文件,同时保持基本文件名即。如果filename1.txt包含20000行,输出将是filename1-1.txt(10000行)和filename1-2.txt(10000行)。
I have a folder that contains multiple text files. I'm trying to split all text files at 10000 line per file while keeping the base file name i.e. if filename1.txt contains 20000 lines the output will be filename1-1.txt (10000 lines) and filename1-2.txt (10000 lines).
我试图使用 split -10000 filename1.txt
但这不是保持基本文件名,我必须重复的文件夹中的每个文本文件的命令。我也试过在<。*。 do split -10000 $ f.txt; done 。这也没有工作。
I tried to use split -10000 filename1.txt
but this is not keeping the base filename and i have to repeat the command for each text file in the folder. I also tried doing for f in *.txt; do split -10000 $f.txt; done
. This didn't work too.
任何想法我怎么能这样做?谢谢。
Any idea how can i do this? Thanks.
推荐答案
for f in filename*.txt; do split -d -a1 -l10000 --additional-suffix=.txt "$f" "${f%.txt}-"; done
或多行写入:
for f in filename*.txt
do
split -d -a1 -l10000 --additional-suffix=.txt "$f" "${f%.txt}-"
done
工作原理:
-
-d
告诉split
使用数字后缀
-a1
>
-l10000
会告诉
用于拆分文件的前缀名。 - additional-suffix = .txt
告诉 split
将 .txt
添加到新文件名称的末尾。--additional-suffix=.txt
tells split
to add .txt
to the end of the names of the new files.$ f
告诉 split
要分割的文件。$ {f%.txt} -
$ c> split
"${f%.txt}-"
tells split
the prefix name to use for the split files.
假设我们从这些文件开始:
Suppose that we start with these files:
$ ls
filename1.txt filename2.txt
然后我们运行命令:
$ for f in filename*.txt; do split -d -a1 -l10000 --additional-suffix=.txt "$f" "${f%.txt}-"; done
完成后,我们现在拥有原始文件和新的拆分文件:
When this is done, we now have the original files and the new split files:
$ ls
filename1-0.txt filename1-1.txt filename1.txt filename2-0.txt filename2-1.txt filename2.txt
使用旧的,较不具有特色的 $ c>
如果您的拆分不提供 - 附加后缀
,请考虑:
for f in filename*.txt
do
split -d -a1 -l10000 "$f" "${f%.txt}-"
for g in "${f%.txt}-"*
do
mv "$g" "$g.txt"
done
done
这篇关于每隔n行拆分批量文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!