0
0

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 1 year has passed since last update.

Ruby を使って気軽に LTSV 形式のログをパースしたい

Last updated at Posted at 2021-11-09
$ cat access.log
time:2015-09-06T05:58:05+09:00  method:POST     uri:/foo/bar?token=xxx&uuid=1234        status:200      size:12 apptime:0.057
time:2015-09-06T06:00:43+09:00  method:GET      uri:/foo/bar    status:400      size:15 apptime:-
time:2015-09-06T06:00:43+09:00  method:GET      uri:/foo/bar/5xx        status:504      size:15 apptime:60.000

# Ruby 2.6 より前でも動くように Hash#to_h にブロックを渡さない書き方をする。
$ cat access.log | ruby -e 'ARGF.read.each_line { |line| hash = line.split("\s").map { |item| item.split(":", 2) }.to_h; puts hash.values_at("method", "uri", "status").join(" ") }'
POST /foo/bar?token=xxx&uuid=1234 200
GET /foo/bar 400
GET /foo/bar/5xx 504
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?