免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2880 | 回复: 5
打印 上一主题 下一主题

[文本处理] 截取时间段和转化为时间戳 [复制链接]

论坛徽章:
20
卯兔
日期:2015-01-26 22:05:142015亚冠之萨济拖拉机
日期:2015-09-10 15:15:282015亚冠之阿尔希拉尔
日期:2015-09-25 17:37:53程序设计版块每日发帖之星
日期:2015-10-03 06:20:00程序设计版块每日发帖之星
日期:2015-12-09 06:20:00CU十四周年纪念徽章
日期:2015-12-17 09:07:15程序设计版块每日发帖之星
日期:2015-12-25 06:20:34程序设计版块每日发帖之星
日期:2015-12-25 06:20:34程序设计版块每日发帖之星
日期:2015-12-25 06:20:342015亚冠之广州富力
日期:2015-08-27 19:29:56每日论坛发贴之星
日期:2015-08-26 06:20:002015亚冠之阿尔希拉尔
日期:2015-05-18 17:26:27
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-04-09 00:13 |只看该作者 |倒序浏览
请教大神一个问题:
输入:
Apr 07, 2015 6:13:30 PM org.apache.coyote.http11.AbstractHttp11Processor endRequest

输出:
Apr 07, 2015 6:13:30 PM

要求:
用awk怎么截取到行首到PM或AM为止的时间,并将该时间段转化为时间戳1428401610,不能在awk里调用date命令

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2015-04-09 01:37 |只看该作者
回复 1# songyc_2015

mktime(datespec)

    Turn datespec into a timestamp in the same form as is returned by systime(). It is similar to the function of the same name in ISO C. The argument, datespec, is a string of the form "YYYY MM DD HH MM SS [DST]". The string consists of six or seven numbers representing, respectively, the full year including century, the month from 1 to 12, the day of the month from 1 to 31, the hour of the day from 0 to 23, the minute from 0 to 59, the second from 0 to 60,54 and an optional daylight-savings flag.

    The values of these numbers need not be within the ranges specified; for example, an hour of -1 means 1 hour before midnight. The origin-zero Gregorian calendar is assumed, with year 0 preceding year 1 and year -1 preceding year 0. The time is assumed to be in the local timezone. If the daylight-savings flag is positive, the time is assumed to be daylight savings time; if zero, the time is assumed to be standard time; and if negative (the default), mktime() attempts to determine whether daylight savings time is in effect for the specified time.

    If datespec does not contain enough elements or if the resulting time is out of range, mktime() returns -1.
   

论坛徽章:
5
未羊
日期:2014-08-04 16:15:21天秤座
日期:2014-08-13 13:52:372015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:112015亚冠之浦和红钻
日期:2015-06-29 15:30:48
3 [报告]
发表于 2015-04-09 09:22 |只看该作者
  1. awk -F "[ ,:]+" '{if($1~/Apr/)$1="04";if($1~/Jan/)$1="01";if($4~/^.$/)$4="0"$4;print strftime("%s",mktime($3" "$1" "$2" "$4" "$5" "$6))}'
复制代码
例子里懒得写那么多$1的月份匹配了,反正都一样LZ自己补齐就是了

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
4 [报告]
发表于 2015-04-09 10:04 |只看该作者
  1. awk 'BEGIN{
  2.     a["Jan"]=01
  3.     a["Feb"]=02
  4.     a["Mar"]=03
  5.     a["Apr"]=04
  6.     a["May"]=05
  7.     a["Jun"]=06
  8.     a["Jul"]=07
  9.     a["Aug"]=08
  10.     a["Sep"]=09
  11.     a["Oct"]=10
  12.     a["Nov"]=11
  13.     a["Dec"]=12
  14.     FS="[ :,]+"
  15. }{
  16.     if($7=="PM") $4=$4+12
  17.     print mktime($3" "a[$1]" "$2" "$4" "$5" "$6)
  18. }
  19. ' file
复制代码

论坛徽章:
28
2015年迎新春徽章
日期:2015-03-04 10:16:53午马
日期:2015-03-20 17:11:07亥猪
日期:2015-03-20 17:11:23戌狗
日期:2015-03-20 17:11:33酉鸡
日期:2015-03-20 17:11:47申猴
日期:2015-03-20 17:11:58未羊
日期:2015-03-20 17:12:10子鼠
日期:2015-03-20 17:12:23丑牛
日期:2015-03-20 17:12:40卯兔
日期:2015-03-20 17:12:51辰龙
日期:2015-03-20 17:13:00巳蛇
日期:2015-03-20 17:13:12
5 [报告]
发表于 2015-04-09 16:07 |只看该作者
  1. echo "Apr 07, 2015 6:13:30 PM org.apache.coyote.http11.AbstractHttp11Processor endRequest"|awk -F'[,: ]' -vT="Jan|Feb|Mar|Apr|May|June|July|Aug|Sep|Oct|Nov|Dec" 'function m (x,y) {for(i=0;i<x;i++)y="0"y;return y}BEGIN{split(T,a,"|");for(;j++<12;){b[a[j]]=m(1-(j/10),j)}}{$5=$8=="AM"?$5:($5+12);c=$4" "b[$1]" "$2" "$5" "$6" "$7;print mktime(c)}' inputfile
复制代码

论坛徽章:
20
卯兔
日期:2015-01-26 22:05:142015亚冠之萨济拖拉机
日期:2015-09-10 15:15:282015亚冠之阿尔希拉尔
日期:2015-09-25 17:37:53程序设计版块每日发帖之星
日期:2015-10-03 06:20:00程序设计版块每日发帖之星
日期:2015-12-09 06:20:00CU十四周年纪念徽章
日期:2015-12-17 09:07:15程序设计版块每日发帖之星
日期:2015-12-25 06:20:34程序设计版块每日发帖之星
日期:2015-12-25 06:20:34程序设计版块每日发帖之星
日期:2015-12-25 06:20:342015亚冠之广州富力
日期:2015-08-27 19:29:56每日论坛发贴之星
日期:2015-08-26 06:20:002015亚冠之阿尔希拉尔
日期:2015-05-18 17:26:27
6 [报告]
发表于 2015-04-10 00:15 |只看该作者
感谢楼上各位大神指导
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP