Skip to main content
 首页 » 编程设计

sed之将大型文本 (xyz) 数据库拆分为 x 等份

2025年05月04日10freeliver54

我想拆分一个大型文本数据库(约 1000 万行)。我可以使用这样的命令

$ sed -i -e '4 s/(dB)//' -e '4 s/Best\ unit/Best_Unit/' -e '1,3 d' '/cygdrive/c/                                                                                                                      Radio Mobile/Output/TRC_TestProcess/trc_longlands.txt' 
 
$ split -l 1000000  /cygdrive/P/2012/Job_044_DM_Radio_Propogation/Working/FinalPropogation/TRC_Longlands/trc_longlands.txt 1 

第一行是清理数据库,接下来是拆分它 - 但是输出文件没有字段名称。我如何将字段名称合并到每个数据集中,并通过管道传输一个列表,其中包含原始文件、新文件名和行号(来自原始文件)。这样就可以在 arcgis 模型中使用它来重新加入最终的简化多边形数据集。

替代和更有用 - 因为这需要进入 arcgis 模型,基于 python 的解决方案是最好的。更多详情请见 https://gis.stackexchange.com/questions/21420/large-point-to-polygon-by-buffer-join-buffer-dissolve-issues#comment29062_21420和 Remove specific lines from a large text file in python

根据 icyrock.com 的回答,使用基于 CYGWIN 的 Python 解决方案

我们有 process_text.sh

cd  /cygdrive/P/2012/Job_044_DM_Radio_Propogation/Working/FinalPropogation/TRC_Longlands 
mkdir processing 
cp trc_longlands.txt processing/trc_longlands.txt 
cd txt_processing 
sed -i -e '4 s/(dB)//' -e '4 s/Best\ unit/Best_Unit/' -e '1,3 d' 'trc_longlands.txt' 
split -l 1000000  trc_longlands.txt trc_longlands_ 
cat > a 
h 
1 
2 
3 
4 
5 
6 
7 
8 
9 
^D 
split -l 3 
split -l 3 a 1 
mv 1aa 21aa 
for i in 1*; do head -n1 21aa|cat - $i > 2$i; done 
for i in 21*; do echo ---- $i; cat $i; done 

如何将“TRC_Longlands”和路径替换为输入文件名 - 在 Python 中,我们为此设置了 %path%/%name。 最后一行是否需要“do echo”?

这是由 python 使用

调用的
import os 
os.system("process_text.bat") 

process_text.bat 基本上是

bash process_text.sh 

从 dos 运行时出现以下错误...

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\georgec>bash P:\2012\Job_044_DM_Radio_Propogation\Working\FinalPropogat ion\TRC_Longlands\process_text.sh 'bash' is not recognized as an internal or external command, operable program or batch file.

同样,当我从 cygwin 运行 bash 命令时,我得到了

georgec@ATGIS25 /cygdrive/P/2012/Job_044_DM_Radio_Propogation/Working/FinalPropogation/TRC_Longlands $ bash process_text.sh : No such file or directory: /cygdrive/P/2012/Job_044_DM_Radio_Propogation/Working/FinalPropogation/TRC_Longlands cp: cannot create regular file `processing/trc_longlands.txt\r': No such file or directory : No such file or directory: txt_processing : No such file or directoryds.txt

但是文件是在根目录下创建的。

为什么有个“.”在目录名之后?如何给他们一个 .txt 扩展名?

请您参考如下方法:

如果您只想将原始文件的第一行添加到除第一个拆分之外的所有行之前,您可以执行以下操作:

$ cat > a 
h 
1 
2 
3 
4 
5 
6 
7 
^D 
$ split -l 3 
$ split -l 3 a 1 
$ ls 
1aa 1ab 1ac a 
$ mv 1aa 21aa 
$ for i in 1*; do head -n1 21aa|cat - $i > 2$i; done 
$ for i in 21*; do echo ---- $i; cat $i; done 
---- 21aa 
h 
1 
2 
---- 21ab 
h 
3 
4 
5 
---- 21ac 
h 
6 
7 

显然,第一个文件将比中间部分少一行,最后一部分也可能更短,但如果这不是问题,这应该可以正常工作。当然,如果你的header行数较多,将head -n1改为head -nX即可,X为header行数。

希望这对您有所帮助。