私は3つのことを試みています。
1#ファイルを小さなチャンクに分割します
2#分割中、各分割ファイルの先頭にテキストを追加する必要があります。 3#各分割ファイルのファイルの最後にテキストを追加する必要があります。
操作を実行する必要がある2 GBのファイルがあります。ここに私が持っているファイルのサンプルの小さなチャンクがあります。このファイルを各10行ごとに分割してください。
操作が必要な生ファイルの例: https://drive.google.com/file/d/0BzQ6rtO2VN95c0YzclhySVZYNDQ/view?usp=sharing
各分割ファイルをphpファイルにしたい。 phpファイルの例を次に示します。 https://drive.google.com/file/d/0BzQ6rtO2VN95ZjU1WXpjTElCaEE/view?usp=sharing
新しく分割したファイルがPHPファイルである必要があるだけで、次のようになります。
<?php
$urls = array("tvproduction.agency
","http://tween.agency","http://twelve.agency","http://twenty1.agency
","http://twenty47.agency
","http://twentynine.agency
","http://twentyone.agency
","http://twentysenses.agency
","http://twentysix.agency
","http://twentyten.agency");
?>
split
を試しましたが、ファイルを分割するだけです。
questionは完全に明確に見えますが、例は私には完全に明確ではありません。したがって、質問の説明に固執します。
以下のスクリプトは、説明したとおりに正確に機能します。そうなる:
さらに:
ファイルの名前は次のとおりです。
split_1, split_2, split_3
等.
次のようなファイル
aap
noot
mies
wim
zus
jet
teun
vuur
gijs
aardappel
pinda
aap
noot
mies
wim
...
次のように分割されます:
something_before
aap
noot
mies
wim
zus
jet
teun
vuur
gijs
aardappel
something_after
等
#!/usr/bin/env python3
# --- set the file, the prefix , suffix and chunksize below
f = "/home/jacob/Bureaublad/test"
prefix = "something_before"
suffix = "something_after"
chunksize = 10
# --- don't change anything below
n = 1
nfile = 1
with open(f) as read:
for l in read:
if (n-1) % chunksize == 0:
s = prefix+"\n"; a = ""
Elif n%chunksize == 0:
a = suffix; s = ""
else:
a = ""; s = ""
open("split_"+str(nfile), "a+").write(s+l+a)
if n%chunksize == 0:
nfile += 1
n += 1
# add the suffix to the last file if it is "incomplete"
last = suffix if a == "" else ""
open("split_"+str(nfile), "a+").write(last)
split_chunks.py
として保存しますスクリプトのheadセクションで、大きなファイルへのパス、prefix、suffix、chunksize(分割する行数)を設定します
# --- set the file, the prefix , suffix and chunksize below
f = "/home/jacob/Bureaublad/test"
prefix = "something_before"
suffix = "something_after"
chunksize = 10
次のコマンドを使用してスクリプトを実行します。
python3 /path/to/split_chunks.py
...続きます、寝る必要があります:)
クォートを正しくするのは少し難しい(I thinksplit
は内部でbash - c ' ... '
を呼び出すため)、しかし最近のバージョンのGNU split
それを可能にする--filter
コマンドを提供する-例えばsed
を使用する
split -l 10 --filter='sed -e '\''1i\header text'\'' -e '\''$a\footer text'\'' > $FILE' file
または(さらに簡単)、printf
split -l 10 --filter='{ printf "header text\n"; cat - ; printf "footer text\n"; } > $FILE' file
例として、与えられたfile
は
printf '%03d\n' {1..100} > file
つまり、001から100までの数字で構成される行、
split -l 10 --filter='{
printf "header line 1\nheader line 2\n"
cat -
printf "footer line 1\nfooter line 2\n"
} > $FILE' file
出力ファイルを与える
$ ls xa?
xaa xab xac xad xae xaf xag xah xai xaj
例えば
$ cat xad
header line 1
header line 2
031
032
033
034
035
036
037
038
039
040
footer line 1
footer line 2