Solved

Very simple bash question

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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