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!
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"?>
<RootNode>
<A node>blah</A>
<Another node>more blah</AnotherNode>
</RootNode>
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.
My first thought would be to try and get your application to output the data more directly. If out has any way to separate its logs, that would probably help.
But I'll assume that you can't. In that case, I would try using sed and regexes to split off the entire multi-line chunk. Parse the xml, then pull off the info you wanted.
But this still screams of hack, so I would definitely search for a better way of getting the data in the first place.
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:
Code:
import datetime
searchFor = "more blah"
while(True):
try:
x = raw_input()
d=x.split()[0]
except Exception:
print "Searched string not found"
break
try :
d = [int(i) for i in d.split('-')]
prevdate = datetime.date(d[0],d[1],d[2])
except:
pass
if searchFor in x :
print "\n found the search key after ", prevdate
break
redirect the log output as input to this script and it will do your job
what say?
Why not use tac (reverse of cat) and then when you find what your looking for you can keep going till you find a date and time at the start of the line.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.