LoginSignup
0
1

More than 5 years have passed since last update.

CSVのゼロパディングを外すワンライナー

Posted at

数字の先頭のゼロを外したいケース。

$ cat test.txt 
1,2,3,0004,5
01,0,0,0002,4

「コンマまたは行頭」がうまく表現できず、sedだとこんな感じ。

$ sed -e 's/,0*\([1-9]\)/,\1/g' test.txt | sed -e 's/^0*\([1-9]\)/\1/g'
1,2,3,4,5
1,0,0,2,4

全ての列が数値、という前提ですが、Rubyならこんな感じにかけます。

$ ruby -ne 'puts $_.split(",").map { |v| v.to_i }.join(",")' test.txt 
1,2,3,4,5
1,0,0,2,4

参考リンク

0
1
1

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
0
1