[SOLVED] Trying to filter lines in a log from last hour
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Looking for some help trying to work with a log file in a CentOS 7.9 machine. I would like to find entries in the log within the past hour. Then I will grep the lines for specific dialog.
The log timestamps are like below
Code:
[2022/04/01 14:27:46.0277603]
I am running into an issue, where trying the below grep for the date range is coming back with nothing. This is the part that needs the halp! :-)
Firstly, you're assuming the date starts at the start of the string, but there's an opening bracket there - to fix that, you can either remove the "^" or start the expression with "^\["
The second issue is that you're missing the $ that will cause your date command to be evaluated, i.e: "$(date -d -1hour +'%Y/%m/%d')"
Thanks, Peter! That got me further along. It is now showing the current day of log entries from midnight on, however. Not sure why it is not picking up just the last hour arg. This is what I am using now
It is now showing the current day of log entries from midnight on, however. Not sure why it is not picking up just the last hour arg.
Run your date command on its own and the first past of the reason would hopefully be obvious: you've specified an output pattern that only consists of year/month/day.
However, you need to note that even if you add time/hours, regex/grep doesn't have a concept of "greater than". You need something like Awk/Perl/etc to achieve that logic (or a specialized tool).
Quote:
syg00 - I will definitely give that a shot as well, feeding the log to the journal!
Again, if that's an option you should do it, then you get a whole bunch of useful and tested functionality you can use.
But, I wouldn't automatically do this for all log files - web server request logs are an example of something I wouldn't want in there.
Still, depending on what task you're trying to achieve, there are other log parsing/management tools that may help you do it easier.
I will try to see how things go with feeding just this particular set of logs to the journal. I figured there were reasons why the app dev didn't want these sent to the journal, but it may worth trying it out. Sure beats crafting the customized regex etc. to get what you need out from the raw log.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.