Solved

Very simple bash question

Posted on 2004-08-16
4
319 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 50 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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