FreeTstar

loving Freedom + + loving Linux

  • 首页
  • Archive
  • 关于
  • 杂七杂八
  • 留言板
Twitter Sina RSS

用sed截取程序日志中某个时间范围内的文本

发布于 2010年12月31日 由 freetstar
14 条评论发表评论

需求:从日志信息中提取特定时间范围内的日志,并将其发送至远程主机上
分析:一般的需求只是以两个特定的时间点做要求,比如说12:00:00到13:00:00这两个准确时间点之间的日志信息,同时日志中肯定是存在这两个时间点的
本需求只是求个范围值,比如说12点到13点之间的日志信息,并不能确定日志中有这两个时间点,也就说有可能只是12:00:01这样的时间点

 #!/bin/bash
#scp today's error.log and profler.log to the mailserver
#author:liangaoxin
#date:20101231
<!--more-->
#working directory
pwd=/u01/lgx/temp
#destination's IP
dst=192.168.33.41
#foemat the date
today=`date '+%Y-%m-%d'`

if [ -e app.log.${today} ]
then
        ###截取6点和8点之间的日志信息,通过来取得时间,如06:00:01,更详细的日志信息格式就不多写了,类似于apache一般的access_log,本脚本也只是个思路而已
      start=`cut -c12-19 app.log.{today}|grep -n "..:..:.."|awk -F: '$2<9 && $2>05 {print $1}'|head -n1`
        stop=`cut -c12-19 app.log.{today}|grep -n "..:..:.."|awk -F: '$2==9 {print $1}'|head -n1`
        sed -n "${start},${stop-1}p" app.log.${today} >app.log.${today}.send
        scp -p /u01/log/temp/app.log.${today}.send lgx@${dst}:/home/lgx/$(hostname)/
fi
要是指定特定时间段内日志信息的提取会简单的多,如下
sed -n '/15:32:30/,/16:12:09/p' access_log >log.txt

祝大家新年快乐~^_^

分类:服务器
贴下vimrc和zshrc
小记:日常软件整理

14 则回应给 “用sed截取程序日志中某个时间范围内的文本”

  1. Mucid UNITED STATES Mozilla Firefox Ubuntu Linux 说道:
    2011年01月1日 7:14 下午

    我好像有些洁癖……总是喜欢搞清理……连日志都不放过……

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    一月 2nd, 2011 at 8:11 下午

    什马意思阿

    [回复]

  2. ihipop CHINA 说道:
    2011年01月1日 10:51 上午

    grep -n 是亮点

    [回复]

    freetstar CHINA Google Chrome Windows 回复:
    一月 1st, 2011 at 3:27 下午

    @ihipop, yes :twisted:

    [回复]

  3. dorole Internet Explorer Windows 说道:
    2010年12月31日 10:26 下午

    :twisted: shell万能!shell无敌!新春快乐!

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    十二月 31st, 2010 at 10:31 下午

    @dorole, 新春快乐 :roll:

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    十二月 31st, 2010 at 10:31 下午

    @dorole, 准确的说是新年快乐哈

    [回复]

    dorole Internet Explorer Windows 回复:
    十二月 31st, 2010 at 10:34 下午

    @freetstar, 也对,年字顺口多了 :twisted:

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    十二月 31st, 2010 at 10:41 下午

    @dorole, :!: 愿大家万事如意

    [回复]

  4. 婉秋 AUSTRALIA Mozilla Firefox Ubuntu Linux 说道:
    2010年12月31日 9:38 下午

    我用过这个调出网络流量
    cat /proc/net/dev | grep eth0 | sed ‘s=^.*:==’ | awk ‘{ print $1 }’

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    十二月 31st, 2010 at 10:19 下午

    @婉秋, 额…这个net/dev我得好好研究下,看样子是网络都走的他阿

    [回复]

  5. 丕子 CHINA Mozilla Firefox Windows 说道:
    2010年12月31日 8:48 下午

    哇哦 你这个代码宽度超出了分辨率了 太宽了 把页面给弄得很宽

    [回复]

    freetstar CHINA Google Chrome Windows 回复:
    十二月 31st, 2010 at 9:05 下午

    @丕子, 我准备换主题。。。 :-?

    [回复]

    Mucid UNITED STATES Mozilla Firefox Ubuntu Linux 回复:
    一月 1st, 2011 at 10:07 上午

    @freetstar, 我开着proxy,所以有时候css加载的慢,就会页面超出……

    [回复]

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用 * 标注

*

*


疑问 开心 悲伤 邪恶 惊叹 微笑 脸红 笑 惊讶 惊奇 迷惑 酷 憨笑 生气 阴险 转眼球 眨眼 主意 箭头 中立 哭 大笑

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image

点击取消回复
第 1 页,共 1 页1
  • 欢迎订阅

  • Github

  • 近期评论

    • freetstar CHINA Mozilla Firefox Linux 发表在《留言板》
    • _Fly CHINA Google Chrome Linux 发表在《使用e4rat加速你的linux启动速度(ext4文件系统专用)》
    • walfred Mozilla Firefox Windows 发表在《留言板》
    • freetstar CHINA Mozilla Firefox Windows 发表在《用landslide创建html5 slide》
  • Spread Firefox Affiliate Button
  • 我的豆瓣

  • 链接表

    • 51开源社区
    • Adam's
    • delectate's blog
    • Don's
    • fl
    • ghostunix
    • Jasey Wang
    • Jerry's Blog
    • Moolight@Ericyue
    • OSMSG
    • panda
    • Pity is the Bomp
    • PT ubuntu
    • Tao Zhu
    • ubuntuhome
    • WOW!ubuntu
    • zhangzhfang
    • 丕字的blog
    • 傻东の学习笔记
    • 奶牛的blog
    • 孤风颠影
    • 弯柚博客
    • 熊猫党军委主席
    • 靖 轩
    • 马力哥
    • 黑孩儿
  • 我的支付宝

  • 功能

    • 登录
    • 文章 RSS
    • 评论 RSS
    • WordPress.org
© FreeTstar. WordPress 强力驱动 | Nest Theme by YChong