LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-11-2017, 04:13 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
Show logs within a certain period of time


I tried using the following:
sed -n '/Apr 10/,/Apr 12/p' /var/log/cron | awk -F: '$1 >= 20 && $2 >= 50 { print }'

Sed displays all lines beginning with the first record from April 10 to the last record of April 11 (don't know exactly how to achieve this, so I wrote "Apr 12", which is tomorrow - I guess I can cut the last line when logs on April 12 will be populated).

The problem is that awk, for some reason, doesn't display all the hours starting with the 20th. It actually displays all hours, from 0 to 23. The weird thing is that the minute works, so $2 >= 50 does display only lines recorded after (and including) the 50th minute. Any idea why it does that?

Thanks
 
Old 04-11-2017, 04:31 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,008
Blog Entries: 3

Rep: Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633
Yes, you are using a colon as the field separator, so the fields are not numbers. You'll want to stick with white space as a field separator and then attack the time manually using split() or similar and then examine the pieces of the resulting array.

Or you could use a pattern for FS and include both white space and a colon in that pattern. You'll have to recalculate which fields to look at then.
 
Old 04-11-2017, 04:41 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,952

Rep: Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073
Usually a good idea to print what you are testing when things go wrong - just so you know.
 
Old 04-11-2017, 04:44 AM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
@Turbocapitalist. Yes, it's obvious now that the first field is going to be "MM DD HH:MM:SS". I completely missed that.

@syg00 By printing, you mean show others what is displayed on the screen?
 
Old 04-11-2017, 04:49 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,952

Rep: Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073
Yep - basic debugging would be to print $1 and $2. Take it out when you get things fixed.
 
Old 04-11-2017, 07:29 AM   #6
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Some log files store the date as the epoch date. If you want to fine tune your range to the minute or second, you might want to convert to epoch date. Otherwise the programmatic logic for the edge cases can get complex. You can use date to do most of that conversion. Although easier said than done as a lot of the comparisons are ascii strings, not numerical.
 
Old 04-13-2017, 09:55 AM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
awk -F '[ :]' '$4 >= 2 && $5 >= 40 {print}' seems to work fine
Together with sed it behaves differently. For some reason, sed deletes one space between the month and the day (e.g. "Apr" and "10"), so I need to use awk but with columns $3 and $4 instead of $4 and $5, which would work if I start processing with awk.

Last edited by vincix; 04-13-2017 at 10:00 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Option to adjust time period Huamin Linux - Software 1 11-20-2015 09:48 PM
Allow access on squid for a time period. Maddrox Linux - Networking 3 01-23-2014 11:40 AM
Command execution for particular period time natarajan Linux - Server 1 12-11-2013 03:44 AM
Calculate Time Period in Scripting anishkumarv Linux - Newbie 10 03-17-2011 08:56 AM
Web Server and IDS logs at the same access time period, what to do for the alert? shahmeer75 Linux - Security 2 05-25-2010 08:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration