1
1

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 5 years have passed since last update.

sedでほしい文字列を抽出する方法

Last updated at Posted at 2016-09-16

先日、ログファイルから、データを抽出するバッチを作りました。
いろいろ試し、助けてもらって、ようやくできました。メモしなきゃ!

やりたいこと

ログファイルから、2016-xx-xxの日付の部分と最後のinsert numberの数字を抽出して、
ファイルに格納します。 

~イメージ~
元のログファイル

log.txt
/home/Logs/log.20160101:2016-01-01 07:32:25 INFO:  insert number [101]
/home/Logs/log.20160102:2016-01-02 07:32:18 INFO:  insert number [202]
/home/Logs/log.20160103:2016-01-03 07:32:20 INFO:  insert number [303]
/home/Logs/log.20160104:2016-01-04 07:32:28 INFO:  insert number [404]
/home/Logs/log.20160105:2016-01-05 07:32:16 INFO:  insert number [505]

結果ファイル

result.txt
2016-01-01 101
2016-01-02 202
2016-01-03 303
2016-01-04 404
2016-01-05 505

実行方法

実行スクリプト

cat log.txt | sed -e 's/.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*insert number \[\([0-9]\+\)\]/\1 \2/g' > result.txt

参考:文字列から特定の文字を抽出する

※ sed 部分の説明

括弧()でデータの範囲は特定できる。
指定の1番と2番目の内容 \1 \2 に置き換える。 (\は、下の画像では円マークで表示)

detail.PNG

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?