Tbpgr Blog

Ruby プログラマ tbpgr(てぃーびー) のブログ

sedでログの特定日時の行範囲を取得する(おまけでRubyによるサンプル日時データ生成)

概要

sedでログの特定日時の行範囲を取得する

内容

大容量のログファイルがあり、日時の昇順で内容が書き込まれていることが前提。
特定の日時範囲のログのみを取得する際にsedを利用する。

コマンド

「2012/11/12 00:27:48」が出力された行から最後の行までを出力

sed -ne '/^2012\/11\/12 00:27:48/,$p' sample.txt

「2012/11/12 00:27:48」が出力された行から「2012/11/12 00:28:12」が出力された行までを出力

sed -ne '/^2012\/11\/12 00:27:48/,/^2012\/11\/12 00:28:12/p' sample.txt

サンプルファイル

2012/11/12 00:27:34
2012/11/12 00:27:35
2012/11/12 00:27:36
2012/11/12 00:27:37
2012/11/12 00:27:38
2012/11/12 00:27:39
2012/11/12 00:27:40
2012/11/12 00:27:41
2012/11/12 00:27:42
2012/11/12 00:27:43
2012/11/12 00:27:44
2012/11/12 00:27:45
2012/11/12 00:27:46
2012/11/12 00:27:47
2012/11/12 00:27:48
2012/11/12 00:27:49
2012/11/12 00:27:50
2012/11/12 00:27:51
2012/11/12 00:27:52
2012/11/12 00:27:53
2012/11/12 00:27:54
2012/11/12 00:27:55
2012/11/12 00:27:56
2012/11/12 00:27:57
2012/11/12 00:27:58
2012/11/12 00:27:59
2012/11/12 00:28:00
2012/11/12 00:28:01
2012/11/12 00:28:02
2012/11/12 00:28:03
2012/11/12 00:28:04
2012/11/12 00:28:05
2012/11/12 00:28:06
2012/11/12 00:28:07
2012/11/12 00:28:08
2012/11/12 00:28:09
2012/11/12 00:28:10
2012/11/12 00:28:11
2012/11/12 00:28:12
2012/11/12 00:28:13
2012/11/12 00:28:14
2012/11/12 00:28:15
2012/11/12 00:28:16
2012/11/12 00:28:17
2012/11/12 00:28:18
2012/11/12 00:28:19
2012/11/12 00:28:20
2012/11/12 00:28:21
2012/11/12 00:28:22
2012/11/12 00:28:23
2012/11/12 00:28:24
2012/11/12 00:28:25
2012/11/12 00:28:26
2012/11/12 00:28:27
2012/11/12 00:28:28
2012/11/12 00:28:29
2012/11/12 00:28:30
2012/11/12 00:28:31
2012/11/12 00:28:32
2012/11/12 00:28:33
2012/11/12 00:28:34
2012/11/12 00:28:35
2012/11/12 00:28:36
2012/11/12 00:28:37
2012/11/12 00:28:38
2012/11/12 00:28:39
2012/11/12 00:28:40
2012/11/12 00:28:41
2012/11/12 00:28:42
2012/11/12 00:28:43
2012/11/12 00:28:44
2012/11/12 00:28:45
2012/11/12 00:28:46
2012/11/12 00:28:47
2012/11/12 00:28:48
2012/11/12 00:28:49
2012/11/12 00:28:50
2012/11/12 00:28:51
2012/11/12 00:28:52
2012/11/12 00:28:53
2012/11/12 00:28:54
2012/11/12 00:28:55
2012/11/12 00:28:56
2012/11/12 00:28:57
2012/11/12 00:28:58
2012/11/12 00:28:59
2012/11/12 00:29:00
2012/11/12 00:29:01
2012/11/12 00:29:02
2012/11/12 00:29:03
2012/11/12 00:29:04
2012/11/12 00:29:05
2012/11/12 00:29:06
2012/11/12 00:29:07
2012/11/12 00:29:08
2012/11/12 00:29:09
2012/11/12 00:29:10
2012/11/12 00:29:11
2012/11/12 00:29:12
2012/11/12 00:29:13

出力結果1

コマンド

sed -ne '/^2012\/11\/12 00:27:48/,$p' sample.txt

結果

2012/11/12 00:27:48
2012/11/12 00:27:49
2012/11/12 00:27:50
2012/11/12 00:27:51
2012/11/12 00:27:52
2012/11/12 00:27:53
2012/11/12 00:27:54
2012/11/12 00:27:55
2012/11/12 00:27:56
2012/11/12 00:27:57
2012/11/12 00:27:58
2012/11/12 00:27:59
2012/11/12 00:28:00
2012/11/12 00:28:01
2012/11/12 00:28:02
2012/11/12 00:28:03
2012/11/12 00:28:04
2012/11/12 00:28:05
2012/11/12 00:28:06
2012/11/12 00:28:07
2012/11/12 00:28:08
2012/11/12 00:28:09
2012/11/12 00:28:10
2012/11/12 00:28:11
2012/11/12 00:28:12
2012/11/12 00:28:13
2012/11/12 00:28:14
2012/11/12 00:28:15
2012/11/12 00:28:16
2012/11/12 00:28:17
2012/11/12 00:28:18
2012/11/12 00:28:19
2012/11/12 00:28:20
2012/11/12 00:28:21
2012/11/12 00:28:22
2012/11/12 00:28:23
2012/11/12 00:28:24
2012/11/12 00:28:25
2012/11/12 00:28:26
2012/11/12 00:28:27
2012/11/12 00:28:28
2012/11/12 00:28:29
2012/11/12 00:28:30
2012/11/12 00:28:31
2012/11/12 00:28:32
2012/11/12 00:28:33
2012/11/12 00:28:34
2012/11/12 00:28:35
2012/11/12 00:28:36
2012/11/12 00:28:37
2012/11/12 00:28:38
2012/11/12 00:28:39
2012/11/12 00:28:40
2012/11/12 00:28:41
2012/11/12 00:28:42
2012/11/12 00:28:43
2012/11/12 00:28:44
2012/11/12 00:28:45
2012/11/12 00:28:46
2012/11/12 00:28:47
2012/11/12 00:28:48
2012/11/12 00:28:49
2012/11/12 00:28:50
2012/11/12 00:28:51
2012/11/12 00:28:52
2012/11/12 00:28:53
2012/11/12 00:28:54
2012/11/12 00:28:55
2012/11/12 00:28:56
2012/11/12 00:28:57
2012/11/12 00:28:58
2012/11/12 00:28:59
2012/11/12 00:29:00
2012/11/12 00:29:01
2012/11/12 00:29:02
2012/11/12 00:29:03
2012/11/12 00:29:04
2012/11/12 00:29:05
2012/11/12 00:29:06
2012/11/12 00:29:07
2012/11/12 00:29:08
2012/11/12 00:29:09
2012/11/12 00:29:10
2012/11/12 00:29:11
2012/11/12 00:29:12
2012/11/12 00:29:13

出力結果2

コマンド

sed -ne '/^2012\/11\/12 00:27:48/,/^2012\/11\/12 00:28:12/p' sample.txt

結果

2012/11/12 00:27:48
2012/11/12 00:27:49
2012/11/12 00:27:50
2012/11/12 00:27:51
2012/11/12 00:27:52
2012/11/12 00:27:53
2012/11/12 00:27:54
2012/11/12 00:27:55
2012/11/12 00:27:56
2012/11/12 00:27:57
2012/11/12 00:27:58
2012/11/12 00:27:59
2012/11/12 00:28:00
2012/11/12 00:28:01
2012/11/12 00:28:02
2012/11/12 00:28:03
2012/11/12 00:28:04
2012/11/12 00:28:05
2012/11/12 00:28:06
2012/11/12 00:28:07
2012/11/12 00:28:08
2012/11/12 00:28:09
2012/11/12 00:28:10
2012/11/12 00:28:11
2012/11/12 00:28:12

おまけ

サンプルデータを作成したRubyスクリプト
※現在日時から1秒単位で100件分のレコードを「YYYY/mm/dd hh:MI:ss」形式で生成

# encoding: Windows-31J
require "date"

@date = DateTime.now
(1..100).each do |num|
  @date = @date + Rational(1,24*60*60)
  puts @date.strftime("%Y/%m/%d %H:%M:%S")
end