?
Solved

Very simple bash question

Posted on 2004-08-16
4
Medium Priority
?
323 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
ID: 11808191

#!/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 200 total points
ID: 11808194
Hi g8ballz0fyre,

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

Sunnycoder
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11808196
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
ID: 11808263
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

831 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