码迷,mamicode.com
首页 > 系统相关 > 详细

shell问题案例及三剑客awk与sed解决实践

时间:2015-04-20 19:08:15      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:sed   awk   

20150418周末小题
原始数据:
17/Apr/2015:09:29:24 +0800
17/Apr/2015:09:30:26 +0800
17/Apr/2015:09:31:56 +0800
18/Apr/2015:09:34:12 +0800
18/Apr/2015:09:35:23 +0800
19/Apr/2015:09:23:34 +0800
19/Apr/2015:09:22:21 +0800
20/Apr/2015:09:45:22 +0800
期望结果:
2015-04-17 09:29:24+0800
2015-04-17 09:30:26+0800
2015-04-17 09:31:56+0800
2015-04-18 09:34:12+0800
2015-04-18 09:35:23+0800
2015-04-19 09:23:34+0800
2015-04-19 09:22:21+0800
2015-04-20 09:45:22+0800

解决实践:来自运维21期悠久之翼 群内分享
[root@web01 ~]# cat oldboy.txt 
17/Apr/2015:09:29:24 +0800
17/Apr/2015:09:30:26 +0800
17/Apr/2015:09:31:56 +0800
18/Apr/2015:09:34:12 +0800
18/Apr/2015:09:35:23 +0800
19/Apr/2015:09:23:34 +0800
19/Apr/2015:09:22:21 +0800
20/Apr/2015:09:45:22 +0800


[root@web01 ~]# awk ‘{sub(/Apr/,"04");split($0,array,"[/: ]") ;printf("%s-%s-%s %s:%s:%s%s\n",array[3],array[2],array[1],array[4],array[5],array[6],array[7])}‘ oldboy.txt 
2015-04-17 09:29:24+0800
2015-04-17 09:30:26+0800
2015-04-17 09:31:56+0800
2015-04-18 09:34:12+0800
2015-04-18 09:35:23+0800
2015-04-19 09:23:34+0800
2015-04-19 09:22:21+0800
2015-04-20 09:45:22+0800
[root@web01 ~]# sed -nr ‘s#(..)/(...)/(....):(..):(..):(..) (.*)#\3-\2-\1 \4:\5:\6\7#;s#Apr#04#p‘ oldboy.txt 
2015-04-17 09:29:24+0800
2015-04-17 09:30:26+0800
2015-04-17 09:31:56+0800
2015-04-18 09:34:12+0800
2015-04-18 09:35:23+0800
2015-04-19 09:23:34+0800
2015-04-19 09:22:21+0800
2015-04-20 09:45:22+0800


本文出自 “老男孩linux运维” 博客,请务必保留此出处http://oldboy.blog.51cto.com/2561410/1636008

shell问题案例及三剑客awk与sed解决实践

标签:sed   awk   

原文地址:http://oldboy.blog.51cto.com/2561410/1636008

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!