LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-27-2013, 08:46 AM   #1
rudydark
LQ Newbie
 
Registered: Nov 2006
Posts: 6

Rep: Reputation: 1
Question find before and after log files too, with date-time in filename.


Greetings!

Just stuck with a problem. I would have to grep through several log files with fixed size rotation and with date and time in their name.
(It means that the rotation time can be considered random.)
(example:
Code:
/path/to/log/logfile-name.log.{yy}-{mm}-{dd}-{hh}-{mm}-{ss}
)

I would like to find the apropriate files by name. There can be several so I would like to use find rather more than ls.

Would like to work with several log files once but the problem is p.e.:
Code:
find /path/to/logs/ -name 'logfile-name.log.$(date +"%Y-%m-%d")--05_[0-6][0-9]_[0-6][0-9]'
finds me the files rotated\started in the 05:00 - 05:59 period.
This part is fine but I would need the preceeding and the following one file too in this list.

Only one before and only one after.

(p.e.: filename.log.yy-mm-dd-04-55-22, {filename.log.yy-mm-dd-05-*.*} , filename.log.yy-mm-dd-06-02-00

The number of files can vary, the previous and next needed files can be with any name.

I would like to grep and (p.e.) wordcount for all the files resulted here.
I would prefer a find solution than an ls one but other solutions are also welcome!

I hope my problem is clearly outlined and I hope you have some fine suggestions to solve it.

Thanks in Advance!
--
R
 
Old 05-27-2013, 07:01 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, ls might be a good idea, because with a filename format like that, the names will sort naturally and you can use the -A & -B flags for grep.
Something like
Code:
ls /path/to/logs/ |sort |grep -A1 -B1 logfile-name.log.$(date +"%Y-%m-%d")-05*
http://linux.die.net/man/1/grep
 
Old 05-28-2013, 07:14 AM   #3
rudydark
LQ Newbie
 
Registered: Nov 2006
Posts: 6

Original Poster
Rep: Reputation: 1
Thanks for the ideas chrism01!

Nice trick to grep the results of te ls command.
But the asterisk shall be added to the path in ls
and the expression shall be quoted unless the shell expands that and no result or not the desired results are coming.
The sort command could be left becouse ls default is to list the files with ordered by name.
Anyhow does not hurt there.

Code:
ls -d1 /path/to/logs/* |sort |grep -A1 -B1 'logfile-name.log.$(date +"%Y-%m-%d")-05*'
could do the trick and gives the full path to files on each line.

I wonder that is there a way to do some similar with find and expressions.
like:
Code:
find ( {expression to return only closest one from the files before} ), \
 ( {expression to return the disered set} ), \
( {expression to return only closest one from the files after} )
Maybe the overal results could be piped for further process by grep and wc if needed.
 
  


Reply

Tags
datetime, filename, find, grep, log



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scan for time/date in a log file rrank5377 Programming 4 06-09-2012 10:04 AM
Log Date/time doesn't change with system time saurabhchokshi Linux - Software 5 07-07-2010 12:27 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
Adding date and time to a log file ltodd2 Linux - General 2 12-17-2008 10:05 AM
Can log files be time stamped? (such as FTP login and transfer log files) bripage Linux - Networking 6 08-08-2002 10:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:19 PM.

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