Need directory listing of file names with ranges that contain dates and times
I have a directory with the following example file names: they are named based on date/time. exampleYYYYMMDDHHMMSSsyslog.log
example20120826184847syslog.log example20120826184848syslog.log example20120826185226syslog.log example20120826193642syslog.log example20120827153241syslog.log I need to get a list of files that are in the range of a user entered START_TIME and END_TIME; for example 20120826184848-20120827153241 (the 26th of August @ x time through the 27th of August @ y time) and I can not figure out how to do this. This directory listing I want to cat into a file so at the end I will have a file (syslog.out) that contains the requested file names. Does this make sense? Thank you for any help...I have a feeling this is relatively easy but I am stuck!!! so any help would be greatly appreciated! Thanks! |
I think this should probably work..
There's most likely a very simple elegant solution, but I didn't think of one. Just set this script in your directory of dated log files, make it executable, and run it. Code:
#!/bin/bash |
Here is a nice little awk script:
Code:
#!/bin/bash Code:
cd dir_with_syslogs |
casualfred's shell script approach is a clever one. You might need to adjust it a bit in case a file name with the exact date and time the user entered, doesn't exist. If you don't mind including an awk program as part of the solution, and if I understood what you wanted, this appears to work do a comparison, whether or not a file exists with a name containing the exact date/time the User enters.
I put the list of file names you provided in a file named data.txt and the following program in a file named date_list.gawk: Code:
( NR == 1 ) { start_date = $1 + 0 ; end_date = $2 + 0 ; } Code:
( echo 20120826184848 20120827153241 ; cat data.txt ) | gawk -f date_list.gawk Code:
example20120826184848syslog.log |
casualfred's soln is a nice shell script one, but for paranoia mode, I'd sort the list
Code:
ls -1 *.log |sort > filelist |
What about this solution
Thank you for the feedback!
I came up with this late last night but it takes a few seconds to run so maybe it is clean on the eye, but not clean running. for i in `ls *2012082919{5923...5963}* 2>/dev/null`; do echo $i >> syslog.out; done Is there some reason not to do it this way? |
Chose the date_list.gawk
The date_list.gawk works much faster than mine! Thanks!
|
Here is one that is completely bash:
Code:
#!/bin/bash Code:
cd dir_with_syslogs |
All times are GMT -5. The time now is 07:16 AM. |