linux 提取日志字段,记一次Linux下提取MySQL日志关键字段

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

环境说明

操作系统:centos7

sed版本:4.2.2

egrep版本:2.20

paste版本:8.22

提取要求

一次同事说,需要提取MySQL日志的关键字段,叫我帮忙,每一行一定包含4个或者7个字段,下面是字段例子

IP(特殊处理为内网IP):192.168.1.1

时间:2018-07-07 19:10:18

文件绝对路径:/home/hms/data/c/rec_pub_73_c/TVOD/88888905/224/3221229835/10000100000000060000000009130764_0.smil.20180707191000.ts

丢包时间:1.659539

上次丢包时间(不一定有):1530961817.271825

当前丢包时间(不一定有):1530961818.935411

码率(不一定有):2540.155518

如上展示,每一行一定会包含4个字段,至于后面3个只能都是有或者都没有

思路

1.删除多余字段,直接得到4个或者7个字段,表达式太过复杂

2.直接过滤出7个想要的字段,因为7个字段不是每一行都出来,基本上不可能实现(至少我想不到怎么写)

3.重组文件,把想要的字段筛选出来,文件合并,按照字段顺序重组文件(待会使用这个思路)

日志样板

点击显/隐内容

192.168.1.1 2018-07-07 19:10:18:935 C06B688SPC013 ts_record.c:11105 code:2 thread:11349 ts_packet_check: Warning: Stream /home/hms/data/c/rec_pub_73_c/TVOD/88888905/224/3221229835/10000100000000060000000009130764_0.smil.20180707191000.ts curr_lose_pkg_time 1.659539 sec,time 1530961817.271825 -> 1530961818.935411 second_rate 2540.155518.

192.168.1.1 2018-07-07 19:15:00:056 C06B688SPC013 ts_record.c:11600 code:2 thread:11349 ts_packet_set_status: /home/hms/data/c/rec_pub_73_c/TVOD/88888905/224/3221229835/10000100000000060000000009130764_0.smil.20180707191000.ts record successfully with total_lose_pkg_time 1.659539 sec.

192.168.1.1 2018-07-07 04:59:25:848 C06B688SPC013 ts_record.c:11105 code:2 thread:21051 ts_packet_check: Warning: Stream /home/hms/data/c/rec_pub_68_dgcp2_c/TVOD/88888888/224/3221229808/10000100000000060000000009083746_0.smil.20180707045500.ts curr_lose_pkg_time 1.920283 sec,time 1530910763.925132 -> 1530910765.848897 second_rate 2952.463135.

192.168.1.1 2018-07-07 05:00:00:067 C06B688SPC013 ts_record.c:11600 code:2 thread:21051 ts_packet_set_status: /home/hms/data/c/rec_pub_68_dgcp2_c/TVOD/88888888/224/3221229808/10000100000000060000000009083746_0.smil.20180707045500.ts record successfully with total_lose_pkg_time 1.920283 sec.

提取字段方法

1、得到 IP 时间

cat hms_log_20180707.log| egrep -o “([0-9]{1,3}.){3}[0-9]{1,3}[[:space:]][0-9]{4}(-[0-9]{2}){2}[[:space:]][0-9]{2}(:[0-9]{2}){2}”

2、得到 文件绝对路径

cat hms_log_20180707.log | egrep -o “/home.*.ts”

3、得到 丢包时间

cat hms_log_20180707.log | egrep -o “[[:space:]][0-9]{1,3}.[0-9]{6,7}[[:space:]]sec” | egrep -o “[0-9]{1,3}.[0-9]{6,7}”

4、获取 上次丢包时间、当前丢包时间、码率(不一定有)

cat hms_log_20180707.log | egrep -o “sec.*” | sed “s/ ->//g” | sed “s/ second_rate//g” | sed “s/.$//g” | sed ‘s/sec,time //g’ | sed ‘s/sec//g’

这里注意一个是,这三个字段不一定有,当不存在时,匹配不到需要保留空白行

提取过程

1、得到 IP 时间

cat hms_log_20180707.log | egrep -o “([0-9]{1,3}.){3}[0-9]{1,3}[[:space:]][0-9]{4}(-[0-9]{2}){2}[[:space:]][0-9]{2}(:[0-9]{2}){2}” > hms_log_20180707.log_1

2、得到 文件绝对路径

cat hms_log_20180707.log | egrep -o “/home.*.ts” > hms_log_20180707.log_2

3、得到 丢包时间

cat hms_log_20180707.log | egrep -o “[[:space:]][0-9]{1,3}.[0-9]{6,7}[[:space:]]sec” | egrep -o “[0-9]{1,3}.[0-9]{6,7}” > hms_log_20180707.log_3

4、获取 上次丢包时间、当前丢包时间、码率(不一定有)

cat hms_log_20180707.log | egrep -o “sec.*” | sed “s/ ->//g” | sed “s/ second_rate//g” | sed “s/.$//g” | sed ‘s/sec,time //g’ | sed ‘s/sec//g’ > hms_log_20180707.log_4

合成包含4或7个字段的文件

paste -d” “ hms_log_20180707.log_1 hms_log_20180707.log_2 hms_log_20180707.log_3 hms_log_20180707.log_4 | sed “s/ $//g” > hms_log_20180707.log_5

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值