Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
This is my first post on here, though I've been tinkering with linux/unix scripts for a while.
I have written a script that searches a file for a string, and then searches backwards for the first date it comes across. THe reason being I need to search across multiple log files on multiple boxes, and find the most recent occurrence of a string. However the date is not on the same line as the string I'm searching for, as I'm searching for something in a multi line XML input.
and as you can see, it grep's the file with line numbers, and for each match, searches backwards from that line number to the previous occurrence. This works, but I can't help feeling there has to be a better way, as it's really inefficient.
Thanks for your reply guys (and sorry for the delay in responding).
The files I am parsing are not XML, they are application log files, so look like:
2011-11-09 11:00:02.234214 aThreadId DEBUG A line of the log file that contains all sorts of stuff
2011-11-09 11:00:02.234314 aThreadId DEBUG Another line of the log file that contains all sorts of stuff
2011-11-09 11:00:02.234214 anotherThreadId DEBUG A line of the log file that contains all sorts of stuff about a different thread
2011-11-09 11:00:02.234214 aThreadId DEBUG XML Import received: <?xml version="1.0" encoding="UTF-8"?>
<Another node>more blah</AnotherNode>
and so on...
Because the XML in the logfile is multiline, if I want to find an input with "more blah", I then need to search up to find the timestamp from the log line at the start of that XML.
Assuming that the aim is to get the task done and not to learn shell scripting,
the task can be accomplished using a small python script like this:
searchFor = "more blah"
x = raw_input()
print "Searched string not found"
d = [int(i) for i in d.split('-')]
prevdate = datetime.date(d,d,d)
if searchFor in x :
print "\n found the search key after ", prevdate
redirect the log output as input to this script and it will do your job