将大文件拆分成块而不拆分条目

use*_*066 8 text-processing split

我有一个相当大的 .msg 文件,格式为 UIEE 格式。

$ wc -l big_db.msg
8726593 big_db.msg
Run Code Online (Sandbox Code Playgroud)

本质上,该文件由各种长度的条目组成,如下所示:

UR|1
AA|Condon, Richard
TI|Prizzi's Family
CN|Collectable- Good/Good
MT|FICTION
PU|G.P. Putnam & Sons
DP|1986
ED|First Printing.
BD|Hard Cover
NT|0399132104
KE|MAFIA
KE|FICTION
PR|44.9
XA|4
XB|1
XC|BO
XD|S

UR|10
AA|Gariepy, Henry
TI|Portraits of Perseverance
CN|Good/No Jacket
MT|SOLD
PU|Victor Books
DP|1989
BD|Mass Market Paperback
NT|1989 tpb g 100 meditations from the Book of Job "This book...help you
NT| persevere through the struggles of your life..."
KE|Bible
KE|religion
KE|Job
KE|meditations
PR|28.4
XA|4
XB|5
XC|BO
XD|S
Run Code Online (Sandbox Code Playgroud)

这是两个条目的示例,由空行分隔。我希望将这个大文件拆分为较小的文件,而不会将条目分成两个文件。

每个单独的条目在文件中由换行符(完全空白行)分隔。我想把这个 870 万行的文件分成 15 个文件。我知道split存在这样的工具,但我不太确定如何拆分文件,但只将其拆分为换行符,因此单个条目不会被分成多个文件。

mik*_*erv 2

这是一个可行的解决方案:

seq 1 $(((lines=$(wc -l </tmp/file))/16+1)) $lines |
sed 'N;s|\(.*\)\(\n\)\(.*\)|\1d;\1,\3w /tmp/uptoline\3\2\3|;P;$d;D' |
sed -ne :nl -ne '/\n$/!{N;bnl}' -nf - /tmp/file
Run Code Online (Sandbox Code Playgroud)

它的工作原理是允许第一个sed编写第二个sed的脚本。第二个sed首先收集所有输入行,直到遇到空行。然后它将所有输出行写入文件。第一个sed为第二个写出一个脚本,指示它在哪里写入输出。在我的测试用例中,脚本如下所示:

1d;1,377w /tmp/uptoline377
377d;377,753w /tmp/uptoline753
753d;753,1129w /tmp/uptoline1129
1129d;1129,1505w /tmp/uptoline1505
1505d;1505,1881w /tmp/uptoline1881
1881d;1881,2257w /tmp/uptoline2257
2257d;2257,2633w /tmp/uptoline2633
2633d;2633,3009w /tmp/uptoline3009
3009d;3009,3385w /tmp/uptoline3385
3385d;3385,3761w /tmp/uptoline3761
3761d;3761,4137w /tmp/uptoline4137
4137d;4137,4513w /tmp/uptoline4513
4513d;4513,4889w /tmp/uptoline4889
4889d;4889,5265w /tmp/uptoline5265
5265d;5265,5641w /tmp/uptoline5641
Run Code Online (Sandbox Code Playgroud)

我是这样测试的:

printf '%s\nand\nmore\nlines\nhere\n\n' $(seq 1000) >/tmp/file
Run Code Online (Sandbox Code Playgroud)

这为我提供了一个 6000 行的文件,如下所示:

<iteration#>
and
more
lines
here
#blank
Run Code Online (Sandbox Code Playgroud)

...重复1000次。

运行上面的脚本后:

set -- /tmp/uptoline*
echo $# total splitfiles
for splitfile do
    echo $splitfile
    wc -l <$splitfile
    tail -n6 $splitfile
done    
Run Code Online (Sandbox Code Playgroud)

输出

15 total splitfiles
/tmp/uptoline1129
378
188
and
more
lines
here

/tmp/uptoline1505
372
250
and
more
lines
here

/tmp/uptoline1881
378
313
and
more
lines
here

/tmp/uptoline2257
378
376
and
more
lines
here

/tmp/uptoline2633
372
438
and
more
lines
here

/tmp/uptoline3009
378
501
and
more
lines
here

/tmp/uptoline3385
378
564
and
more
lines
here

/tmp/uptoline3761
372
626
and
more
lines
here

/tmp/uptoline377
372
62
and
more
lines
here

/tmp/uptoline4137
378
689
and
more
lines
here

/tmp/uptoline4513
378
752
and
more
lines
here

/tmp/uptoline4889
372
814
and
more
lines
here

/tmp/uptoline5265
378
877
and
more
lines
here

/tmp/uptoline5641
378
940
and
more
lines
here

/tmp/uptoline753
378
125
and
more
lines
here
Run Code Online (Sandbox Code Playgroud)