web-dev-qa-db-ja.com

サフィックスとプレフィックスを分割してファイルコンテンツに追加する

私は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を試しましたが、ファイルを分割するだけです。

3
Jaffer Wilson

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)

使い方

  1. スクリプトを空のファイルにコピーし、split_chunks.pyとして保存します
  2. スクリプトの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
    
  3. 次のコマンドを使用してスクリプトを実行します。

    python3 /path/to/split_chunks.py
    

説明

...続きます、寝る必要があります:)

3
Jacob Vlijm

クォートを正しくするのは少し難しい(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
3
steeldriver