Solved

Very simple bash question

Posted on 2004-08-16
4
312 Views
Last Modified: 2010-04-21
This has to be an easy question, I am currently brain-fried from doing other bash scripting...

I have a file that logs timestamps every 20 minutes...
I an error occurs on the system it is logged in this file...
Example:
2004-08-14 22:38:25: ------ TimeCheck - Sat Aug 14 22:40:00 2004
2004-08-14 22:58:25: ------ TimeCheck - Sat Aug 14 23:00:00 2004
2004-08-14 23:18:24: ------ TimeCheck - Sat Aug 14 23:20:00 2004
2004-08-14 23:25:09: ERROR Message goes here
2004-08-14 23:25:10: Additional error info here
2004-08-14 23:25:12: and here
2004-08-14 23:25:12: and here
2004-08-14 23:25:12: and here

etc.. etc...
I need, in my script to be able to cat this file, find the last TimeCheck, report the last TimeCheck, then print out the next 15 lines or so...

I don't mind using awk or sed or anything!!! Just has to be in a bash script...
I could work on this for another hour or so, but I need some sleep..

Thanks in advance.
0
Comment
Question by:g8ballz0fyre
  • 2
4 Comments
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility

#!/bin/bash

# error checking needs completion!
filename=$1

awk '/TimeCheck/{i=0;trc=1}{ if( trc == 1 && i != 16 ){ last_15[i++] = $0}}}END{for( j = 0; j != i; j++){ print last_15[j]}}' $filename
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
Comment Utility
Hi g8ballz0fyre,

tac logfile |  grep "time stamp"  -B 15 -m 1

Sunnycoder
0
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility
Sorry, too many brackets in the awk program - here's the correct one...

awk '/TimeCheck/{i=0;trc=1}{ if( trc == 1 && i != 16 ){ last_15[i++] = $0}}END{for( j = 0; j != i; j++){ print last_15[j]}}'
0
 
LVL 11

Expert Comment

by:lbertacco
Comment Utility
Sunnycoder, I'd pipe the output to tac again to get lines the original order:

tac logfile |  grep "TimeCheck"  -B 15 -m 1 | tac
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now