sedを使用して大容量ファイルヘッダを効率的に削除しますか?

sedを使用して大容量ファイルヘッダを効率的に削除しますか?

次のコマンドは、ファイルのサイズによっては数分かかることがあります。より効率的な方法がありますか?

sed -i 1d large_file 

ベストアンサー1

次に変更してみてくださいed

ed <<< $'1d\nwq' large_file

「大きい」が約1000万行以上を意味する場合は、使用する方が良いですtail。内部編集はできませんが、パフォーマンス上、これらの欠陥は許すことができます。

tail -n +2 large_file > large_file.new

編集するいくつかの視差を表示します。

awkJaypalが追加したコードは、同じマシン(CPU 2.2GHz)で実行時間があります。)

bash-4.2$ seq 1000000 > bigfile.txt # further file creations skipped

bash-4.2$ time sed -i 1d bigfile.txt
time 0m4.318s

bash-4.2$ time ed -s <<< $'1d\nwq' bigfile.txt
time 0m0.533s

bash-4.2$ time perl -pi -e 'undef$_ if$.==1' bigfile.txt
time 0m0.626s

bash-4.2$ time { tail -n +2 bigfile.txt > bigfile.new && mv -f bigfile.new bigfile.txt; }
time 0m0.034s

bash-4.2$ time { awk 'NR>1 {print}' bigfile.txt > newfile.txt && mv -f newfile.txt bigfile.txt; }
time 0m0.328s

おすすめ記事