LoginSignup
41
32

More than 5 years have passed since last update.

bash でゼロパディングされた数字列を作成する

Posted at

ゼロパディングとは、数字の頭を 0 で詰めて、桁数を調整することです。

linux や mac os のターミナルで、数字の列を作成したい場合、seqコマンドが便利です。

$ seq 10
1
2
3
4
5
6
7
8
9
10

seq コマンド自身にもゼロパディングするオプションがあります。

$ seq -w 10
01
02
03
04
05
06
07
08
09
10

パディングの 0 の個数を増やしたい(桁数を増やしたい場合は)-fオプションを使います。

$ seq -f %03g 10
001
002
003
004
005
006
007
008
009
010

-f のフォーマットは

$ man printf 3`

で調べられます。

-f オプションを知るまでは

$ seq 10 | xargs -P1 printf '%03g\n'
001
002
003
004
005
006
007
008
009
010

とか、

$ seq -w 100 | head -n 10
001
002
003
004
005
006
007
008
009
010

とかやっていた時代もありました。

やはり man を読めば、大体やりたいことは出来るようになっているものですね。

41
32
3

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
41
32