LoginSignup
4
5

More than 5 years have passed since last update.

LTSV から特定の項目を特定の並び順で TSV にするワンライナー

Last updated at Posted at 2014-06-18

ワンライナーと言っても ruby ですが。

ltsv.log から ipaddr と status と time をこの並び順で取り出して TSV にする。

$ cat ltsv.log|ruby -F"\t" -nae '$F.map!{|v|v.split":",2};puts %w{ipaddr status time}.map{|v|($F.assoc(v)||[])[1]}.join"\t"'

-l オプションを思いだしたので一文字短くなりました。

$ cat ltsv.log|ruby -F"\t" -plae '$F.map!{|v|v.split":",2};$_=%w{ipaddr status time}.map{|v|($F.assoc(v)||[])[1]}.join"\t"'

Hash[[[1,2],[3,4]]] #=> {1=>2, 3=>4} という書き方を知ったのでさらに少し短くなりました。

$ cat ltsv.log|ruby -F"\t" -plae 'h=Hash[$F.map{|v|v.split":",2}];$_=%w{ipaddr status time}.map{|v|h[v]}.join"\t"'
4
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
4
5