9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

なんてステキな split コマンド

Last updated at Posted at 2021-05-17

Google Search Console で読み込むことができる sitemap.xml は 1ファイル 50,000URL が上限となっています。 See: サイトマップの作成と送信
sitemap.xml を作るための、元の URL のリストファイルがウン十万行とあったので、これを 5万行ごとに分割してから、 sitemap.xml を作ることにしました。

そんな巨大なテキストファイルを分割するときに、大活躍なのが split コマンドです。

split コマンドは MacOS に入ってます。 (BigSurで確認)
Amazon Linux の場合は 1 にも 2 にも入ってます。

ウン十万行の url_list.txt ファイルを
5万行ずつの
url_list00.txt
url_list01.txt
url_list02.txt
url_list03.txt
....
に分割したいときは、以下のようにします。

split -l 50000 -d --additional-suffix=.txt url_list.txt url_list

-l オプションは分割した1ファイルの行数です。
-d オプションは接尾辞を数字の連番で付与します。
--additional-suffix=SUFFIX は上記末尾の後に付与する文字列です。拡張子を付けたい場合はこのオプションで。
そのあとに分割するファイル名、
そのあとにファイル分割後の接頭辞(省略可)です。

split -l 行数 [-d] [--additional-suffix=SUFFIX] ファイル [分割後ファイルの接頭辞]

かなりの行数でも split コマンドなら爆速で処理終了です。

AWS EC2 t3.small(gp3) で、125万行近いファイルを 25個のファイルに分割したときの time の結果がこちら。

real    0m0.038s
user    0m0.012s
sys     0m0.004s

なんて速いんだ!

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?