標籤:sed awk
20150418周末小題未經處理資料:17/Apr/2015:09:29:24 +080017/Apr/2015:09:30:26 +080017/Apr/2015:09:31:56 +080018/Apr/2015:09:34:12 +080018/Apr/2015:09:35:23 +080019/Apr/2015:09:23:34 +080019/Apr/2015:09:22:21 +080020/Apr/2015:09:45:22 +0800期望結果:2015-04-17 09:29:24+08002015-04-17 09:30:26+08002015-04-17 09:31:56+08002015-04-18 09:34:12+08002015-04-18 09:35:23+08002015-04-19 09:23:34+08002015-04-19 09:22:21+08002015-04-20 09:45:22+0800解決實踐:來自營運21期悠久之翼 群內分享[[email protected] ~]# cat oldboy.txt 17/Apr/2015:09:29:24 +080017/Apr/2015:09:30:26 +080017/Apr/2015:09:31:56 +080018/Apr/2015:09:34:12 +080018/Apr/2015:09:35:23 +080019/Apr/2015:09:23:34 +080019/Apr/2015:09:22:21 +080020/Apr/2015:09:45:22 +0800[[email protected] ~]# 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+08002015-04-17 09:30:26+08002015-04-17 09:31:56+08002015-04-18 09:34:12+08002015-04-18 09:35:23+08002015-04-19 09:23:34+08002015-04-19 09:22:21+08002015-04-20 09:45:22+0800[[email protected] ~]# sed -nr ‘s#(..)/(...)/(....):(..):(..):(..) (.*)#\3-\2-\1 \4:\5:\6\7#;s#Apr#04#p‘ oldboy.txt 2015-04-17 09:29:24+08002015-04-17 09:30:26+08002015-04-17 09:31:56+08002015-04-18 09:34:12+08002015-04-18 09:35:23+08002015-04-19 09:23:34+08002015-04-19 09:22:21+08002015-04-20 09:45:22+0800
本文出自 “老男孩linux營運” 部落格,請務必保留此出處http://oldboy.blog.51cto.com/2561410/1636008
shell問題案例及三劍客awk與sed解決實踐