【实例】正则表达式利用grep和sed处理日志内容,
2014-09-11 00:00:01,516 INFO [com.tt.bb.thread.Control] - Socket连接:/182.105.83.33:53217
2014-09-12 10:00:06,555 INFO [com.tt.bb.thread.Control] - Socket连接:/182.113.83.51:53249
2014-09-13 20:00:11,587 INFO [com.tt.bb.thread.Control] - Socket连接:/182.114.83.66:53281
2014-09-14 15:09:16,611 INFO [com.tt.bb.thread.Control] - Socket连接:/182.115.83.17:53313
2014-09-14 15:10:01,408 INFO [com.tt.bb.thread.Control] - Socket连接:/182.115.83.17:53523
2014-09-15 04:01:21,648 INFO [com.tt.bb.thread.Control] - Socket连接:/182.116.83.58:53311
2014-09-15 04:10:21,631 INFO [com.tt.bb.thread.Control] - Socket连接:/182.116.83.58:53551
2014-09-15 04:20:21,608 INFO [com.tt.bb.thread.Control] - Socket连接:/182.116.83.58:53301
2014-09-15 04:30:21,655 INFO [com.tt.bb.thread.Control] - Socket连接:/182.116.83.58:53336
需求1:只获取同一日期,有且只有1个IP地址(去重复)。
解决步骤:
此方法,在sed命令中使用了正则表达式,虽然复杂一点,但是值得倡导。
#cat app.log |sed 's# \([0-9]\{2\}\):\([0-9]\{2\}\).*连接:/# #g'
2014-09-11 182.105.83.33:53217
2014-09-12 182.113.83.51:53249
2014-09-13 182.114.83.66:53281
2014-09-14 182.115.83.17:53313
2014-09-14 182.115.83.17:53523
2014-09-15 182.116.83.58:53311
2014-09-15 182.116.83.58:53551
2014-09-15 182.116.83.58:53301
2014-09-15 182.116.83.58:53336
#cat app.log |sed 's# \([0-9]\{2\}\):\([0-9]\{2\}\).*连接:/# #g'|sed 's#:\([0-9]\{4,\}\)##g'
2014-09-11 182.105.83.33
2014-09-12 182.113.83.51
2014-09-13 182.114.83.66
2014-09-14 182.115.83.17
2014-09-14 182.115.83.17
2014-09-15 182.116.83.58
2014-09-15 182.116.83.58
2014-09-15 182.116.83.58
2014-09-15 182.116.83.58
#cat app.log |sed 's# \([0-9]\{2\}\):\([0-9]\{2\}\).*连接:/# #g'|sed 's#:\([0-9]\{4,\}\)##g'|uniq
2014-09-11 182.105.83.33
2014-09-12 182.113.83.51
2014-09-13 182.114.83.66
2014-09-14 182.115.83.17
2014-09-15 182.116.83.58
需求2:只获取同一日期、同一时间段(取到“时分”位置),有且只有1个IP地址(去重复)。
解决步骤:
此方法,在sed命令中使用了正则表达式,虽然复杂一点,但是值得倡导。
#cat app.log |sed 's#:\([0-9]\{2\}\),\([0-9]\{3\}\).*连接:/# #g'
2014-09-11 00:00 182.105.83.33:53217
2014-09-12 10:00 182.113.83.51:53249
2014-09-13 20:00 182.114.83.66:53281
2014-09-14 15:09 182.115.83.17:53313
2014-09-14 15:10 182.115.83.17:53523
2014-09-15 04:01 182.116.83.58:53311
2014-09-15 04:10 182.116.83.58:53551
2014-09-15 04:20 182.116.83.58:53301
2014-09-15 04:30 182.116.83.58:53336
#cat app.log |sed 's#:\([0-9]\{2\}\),\([0-9]\{3\}\).*连接:/# #g'|sed 's#:\([0-9]\{4,\}\)##g' | uniq2014-09-11 00:00 182.105.83.332014-09-12 10:00 182.113.83.512014-09-13 20:00 182.114.83.662014-09-14 15:09 182.115.83.172014-09-14 15:10 182.115.83.172014-09-15 04:01 182.116.83.582014-09-15 04:10 182.116.83.582014-09-15 04:20 182.116.83.582014-09-15 04:30 182.116.83.58
原文链接:https://www.f2er.com/regex/361277.html