Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

やりたいこと

ログファイルから、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

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away