Monday, 10 November 2008

awk: outputting the last ten minutes of a log file

OK, I've got this log file that has the date/time stamp in field one ($1) and I just wanted to extract the last ten minutes worth of data. Using the gawk functions mktime() and strftime() I wrote the following solution:

Getting the last ten minutes (600 seconds) of a log file:

awk -F, '{ tenago = strftime("%H:%M:%S", mktime(strftime("%Y %m %d %H %M %S", systime() - 600))); if ( substr($1,14) > tenago ) print $0 }' logfile.txt

Getting Hours:Minutes:Seconds timestamp for ten minutes ago was accomplished using:

tenago = strftime("%H:%M:%S", mktime(strftime("%Y %m %d %H %M %S", systime() - 600)))

It makes sense if you read it from the inside out. The systime() function, because it hasn't been supplied any parameters returns the current datetime minus (-) 600 seconds. It's formatted as a string using: "%Y %m %d %H %M %S" and passed to the mktime() function where it's converted to a string again... I think you may be able to simplify this - let me know if you can! :)

EDIT: Here we go, I knew there was a way to simplify the above command! Here's my latest version:

awk -F, '{ tenago = strftime("%H:%M:%S", systime() - 600); if ( substr($1,14) > tenago ) print $0 }'

37 comments:

1023 said...

一個人的際遇在第一次總是最深刻的,有時候甚至會讓人的心變成永遠的絕緣。.........................

vu/34mp4 said...

一個人想法的大小,決定他成就的大小。 ....................................................

無尾熊可愛 said...

I love readding, and thanks for your artical. ........................................

佳慧 said...

破解色情守門人成人色情電影院線上免費色情短片色情動畫影成人色情動畫成人電影秀亞洲線上免費成人電影性片免費狂看性交線上免費影片性免費小說網性美女做愛性美女遊戲性教影片性教學影片性感乳房影片性感做愛電影性感援交妹性感電影性感劍士性愛a片免費試看性小遊戲幸愛影片姓愛圖貼亞洲線上免費電影亞洲線上免費影片其色制聊天室咆哮入口咆哮老鼠網咆哮鼠85cc聊天室情色視訊情色視訊

謝怡臻 said...

若對自己誠實,日積月累,就無法對別人不忠了。........................................

v辰原 said...

成人色情圖片激情聊天室火辣美眉520sex赤裸美女成人自拍貼圖18限性影片觀賞av色情影音聊天手淫激情成人聊天室成人色情圖片網sex520自拍走光照片淫慾18禁成人影音聊天美女台灣性網限制級女生手淫成人聊天春宮裙底風光情趣丁字褲極度震撼情色論壇露點成人視訊聊天avlive show愛愛明星露點台灣色情網站自慰少婦成人聊天網美女視訊成人頻道人妻熟女蕩婦一對多激情成人色情聊天室av成人上空秀巨奶視訊網愛聊天室一夜正妹色情貼片一對多免費美女視訊粉紅乳頭作愛影片美女聊天sexy成人色情圖片網女人胸部圖片視訊色情情色網站性關係辣妹聊天室大奶子

RafaelLetso21555 said...

may the blessing be with you.........................................

裕以 said...

憂能傷身,保重哦! ....................................................

美書 said...

感謝分享好的作品~~........................................

許柏信 said...

I love readding, and thanks for your artical. ........................................

韋于倫成 said...

這麼優的部落格一定要持續下去!........................................

倩亮 said...

ut女聊天台中聊天一夜聊天室uthome聊天ut網際空間聊天搜樂論談新中部聊天室新免費成人影片新性愛影片暗黑破壞神程式暗黑程式豦美鳳豦美鳳光碟圖片台灣論壇圖片區85論壇彰化人留言板彰化人聊天事彰化交友聊天室彰化聊天是熊熊貼圖區熊貓[情色貼圖熊貓av貼圖熊貓人貼圖熊貓人圖片熊貓人影片熊貓天堂圖片85論壇嘟嘟線上影片觀賞嘟嘟網線上免費a片豦美鳳性愛光碟蜜雪兒免費小說 色ˋ情小說 女同志聊天室bt成人

HaroldM22 said...

Venture a small fish to catch a great one. ............................................................

紫倫妍勳 said...

you‘ve got a great personality!............................................................

asda said...

請繼續發表好文!加油加油加油! .........................................

冠慧 said...

一沙一世界,一花一天堂,掌中握無限,剎那即永恆......................................................................

馨裕 said...

這麼好的部落格,以後看不到怎麼辦啊!!.................................................................

常映 said...

生存乃是不斷地在內心與靈魂交戰;寫作是坐著審判自己。......................................................................

志穎志穎 said...

到處逛逛~~來繞繞留個言囉~~~~.................................................................                           

嘉琬嘉琬 said...

所有的資產,在不被諒解時,都成了負債............................................................

雲亨雲亨雲亨 said...

如果相遇.你會感到相知.那麼.有一種習慣叫做陪伴;如果陪伴.你會感到珍惜.那麼.有一種甜蜜叫做存在!..................................................................

天花天花 said...

時間可證明一切~你的BLOG是我覺得不賴的..................................................................

智宜宜 said...

rain before seven; fine before eleven.............................................................

李冠文 said...

HELLO~幫你推個文^^..................................................................

婷珊 said...

Never put both feet in your mouth at the same time, because then you will not have a leg to stand on.............................................................

幸雨幸雨 said...

友誼能增進快樂,減少痛苦............................................................

幼禎幼禎 said...

haha~ funny! thank you for your share~............................................................

楊儀卉 said...

your artical is so funny!! it make me so happy!!............................................................

李威昌v彥霖 said...

大肚能容,了卻人間多少事,滿腔歡喜,笑開天下古今愁。..................................................

惠敏謝 said...

困難的不在於新概念,而在於逃避舊有的概念。............................................................

515152 said...

Lets cross the bridge when we come to it............................................................

凱v胡倫 said...

一時的錯誤不算什麼,錯而不改才是一生中永遠且最大的錯誤..................................................

吳淑惠吳淑惠吳淑惠吳淑惠 said...

當我微笑時,世界和我一起微笑;當我快樂時,世界和我一起活躍。..................................................

蔡曼鄭美玉屏 said...

一棵樹除非在春天開了花,否則難望在秋天結果。..................................................

梁名劉明倫裕 said...

你文章很棒的~繼續分享給大家~~~~..................................................

佳張張張張燕張張張張張 said...

Many a true word is spoken in jest..................................................................

翊翊翊翊張瑜翊翊翊 said...

看著你的BLOG 好多朋友都回應 真厲害............................................................