結果を収集するために多数のファイル(300,000以上)を処理するより効率的な方法は何ですか?

結果を収集するために多数のファイル(300,000以上)を処理するより効率的な方法は何ですか?

fields.txt次の行を含むファイルがありますL=300k+

field1 field2 field3
field1 field2 field3
field1 field2 field3
... 
field1 field2 field3

同じフォルダに、間に名前が付けられた文字列N(識別しましょうs(n))のみを含むファイルがあります。しかし、。res-0-n-0n0LN < L

res_numbers_sorted.tmpコマンドを使用して、上記の数字でソートされたリストを含むファイルを生成しました。n(最も効率的であるかどうかはわかりませんが、かなり速く、他の目的に合わせる必要があります。)

find -maxdepth 1 -type f -name "res-0-*" | sort -t'-' -k3 -n | awk -F'-' '{print $3}'>| res_numbers_sorted.tmp

ファイルはres_numbers_sorted.tmp次のようになります。

0
1
8
... 
299963

結局のところ、私が望むのは、次results.txtのファイル名です。

field1 field2 field3 s(0)
field1 field2 field3 s(1)
field1 field2 field3
...
field1 field2 field3 s(299963) 
...
field1 field2 field3

これはs(n)n番目の文字列に含まれる文字列ですres-0-n-0

cp fields.txt resutls.txt私は最初に次のループを通して私が望むことを達成しましたwhile

while IFS='' read -r line; do 
     #storing the content of the file in a variable
     res=$(<res-0-"$line"-0)     
     # this is needed in order to take into account that sed addresses the first line of a file with the number 1 whereas the file list starts with 0
     real_line=$(( line + 1 ))     
     sed -i "${real_line}s/.$/ ${res}/" field.txt
done < res_numbers_sorted.tmp

しかし、速度が非常に遅いので、何度も実行する必要があります。私はこれがsed仕事に適したツールではないかもしれないと思います。

ベストアンサー1

私が正しく理解したら、行がたくさんあるfields.txtファイルがあります。ファイルが複数ありますres-0-n-0。そして、各行ごとにファイルの内容fields.txtにコピーします(存在する場合)。results.txtres-0-<line_number>

私は単にfields.txtファイルを1行ずつ読み、results.txt必要に応じてファイルの内容の行をエコーできると思います。res-0-<line_number>

私は次のようなものを選択します。

#! /bin/sh

LINE_NUMBER=0
while read line;
do
  if [ -f "res-0-$LINE_NUMBER-0" ]
  then
    echo "$line $(cat res-0-$LINE_NUMBER-0)" >> result.txt
  else
    echo "$line" >> result.txt
  fi
  ((LINE_NUMBER++))
done < fields.txt

おすすめ記事