Finding/Grep on files with date and hour in the file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding/Grep on files with date and hour in the file name
# 1  
Old 06-13-2012
Question Finding/Grep on files with date and hour in the file name

Hi,
I have a folder structure as follows,

DATA -> 2012-01-01 -> 00 -> ABC_2012-01-03_00.txt
-> 01 -> ABC_2012-01-03_01.txt
-> 02 -> ABC_2012-01-03_02.txt
...
-> 23 -> ABC_2012-01-03_02.txt
-> 2012-01-02
-> 2012-01-03

So the dir DATA contains the above hierarchy,
User input Start and End Data
I want to grep on all those files between the input datesSmilie, I want a list of files which come under the input dates.
note that the files creation or modified date is not the date and hour contained in the file name so cannot use find newer/modified command.

So what could be the easiest way of doing it, After finding the files i want to grep a string "PQRS" on the selected files for the input dates.

Any help on this would be highly appreciated...!!! TIA

Last edited by mihirvora16; 06-13-2012 at 01:44 AM..
# 2  
Old 06-13-2012
If your grep supports the -r (recursive search) option try this:

find_data.sh
Code:
#!/bin/bash
if [ $# -eq 0 -o $# -gt 3 ]
then
    echo "usage: $0 pattern [from-date [to-date]]">&2
    exit 2
fi
 
PATTERN=$1
F=${2:-0}
F=${F//-/}
T=${3:-99999999}
T=${T//-/}
 
for subdir in DATA/*
do
   DT=$( basename $subdir )
   DT=${DT//-/}
   [ $DT -lt $F -o $DT -gt $T ] && continue
   echo $subdir
done | xargs grep $PATTERN -r

Call it like this:
Code:
$ find_data.sh my_grep_pattern 2012-01-01 2012-06-13


Last edited by Chubler_XL; 06-13-2012 at 02:31 AM.. Reason: Check input parameters and display usage
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 06-13-2012
Lightbulb

Thanks for the quick reply Chubler_XL let me try this out.

---------- Post updated at 11:46 AM ---------- Previous update was at 11:40 AM ----------

Quote:
Originally Posted by Chubler_XL
If your grep supports the -r (recursive search) option try this:

find_data.sh
Code:
#!/bin/bash
# find_data.sh
# called with:   find_data.sh  pattern [from-data] [to-date]
PATTERN=$1
F=${2:-0}
F=${F//-/}
T=${3:-99999999}
T=${T//-/}
 
for subdir in DATA/*
do
   DT=$( basename $subdir )
   DT=${DT//-/}
   [ $DT -lt $F -o $DT -gt $T ] && continue
   echo $subdir
done | xargs grep $PATTERN -r

Call it like this:
Code:
$ find_data.sh my_grep_pattern 2012-01-01 2012-06-13


Now That worked pretty well, I was not aware of the for loop u mentioned above i used to doe ls -lR and then loop over... but i have multiple files inside the hour directory (00->) so i just want to look up into ABC_ file and not any other files since my search patter is also present under multiple files in the directory.
# 4  
Old 06-13-2012
So are you saying you need to include hour in the from/to criteria?
# 5  
Old 06-13-2012
No i dont want hour in the search criteria basically i want to loop over all the hours of the day but just the pattern search should happen over a selected file i.e. ABC_<Date>_<Hour>.txt as the file is present under all the hour dirs under a specific date...
Its like 00-23 hours for a day dir and ABC_* file under all the hours dir...
got me?
# 6  
Old 06-13-2012
Yes add --include "ABC_*" to grep line:

was
Code:
done | xargs grep $PATTERN -r

now
Code:
done | xargs grep $PATTERN -r --include "ABC_*"

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 06-13-2012
Yeah that works just had to do --include great going mate... really appreciate...
Thanks,...

Could you just point me to any advanced learning guided online? PM me the same. Thanks again

---------- Post updated at 12:05 PM ---------- Previous update was at 11:59 AM ----------

Hey chubler_XL
could you explain in brief how exactly the above thing works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep in a log file within a time range (hour)

Hi, im trying to write a grep script that returns me the last inputs added in the last hour in the log file. Literally i have nothing yet but: grep 'Line im looking for' LOGFILE.log | tail -1 this only gives me the last input, but no necessarily from the last hour. Help Please. (4 Replies)
Discussion started by: blacksteel1988
4 Replies

2. UNIX for Advanced & Expert Users

Finding/Grep on files with date and hour in the file name

Hi, I have a folder structure as follows, DATA -> 2012-01-01 -> 00 -> ABC_2012-01-03_00.txt -> 01 -> ABC_2012-01-03_01.txt -> 02 -> ABC_2012-01-03_02.txt ... -> 23 -> ABC_2012-01-03_02.txt -> 2012-01-02 ... (1 Reply)
Discussion started by: mihirvora16
1 Replies

3. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

4. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

5. Shell Programming and Scripting

finding the files based on date..

Hi to every one , i had ascenario like this.. i had path like export/home/pmutv/test/ in this i will recive 43 files daily with each file having that days date i.e like product.sh.20110512 like this i will 43 files every day i had to find the files. if files are avaliable i... (2 Replies)
Discussion started by: apple2685
2 Replies

6. Shell Programming and Scripting

Finding files for a date

Hi, I have a directory with files from past 2 years.I need to find all the files for a specific date . Due to certain reasons I could not use this command in my process find . -type f \( -newer /tmp/May-1-2007 -a ! -newer... (4 Replies)
Discussion started by: Raamc
4 Replies

7. Shell Programming and Scripting

Grep string from logs of last 1 hour on files of 2 different servers and calculate count

Hi, I am trying to grep a particular string from the files of 2 different servers without copying and calculate the total count of its occurence on both files. File structure is same on both servers and for reference as follows: 27-Aug-2010... (4 Replies)
Discussion started by: poweroflinux
4 Replies

8. Shell Programming and Scripting

Compare file timestamp with current date. Diff must be 1 hour.

Hello, I've created the script below to compare the content of two files with a delay of an hour. After an hour, the lines that exist in both files, will be printed and executed. The script now uses a counter to countdown 50 minutes. But what I would prefer is to check the file timestamp of... (3 Replies)
Discussion started by: taipan
3 Replies

9. Shell Programming and Scripting

Get date and time for past 1 hour from current date

Hi, I need to get the date and time for past 1 hour from the current date. Anyone know how to do so? Thanks (5 Replies)
Discussion started by: spch2o
5 Replies

10. Shell Programming and Scripting

Finding mail per hour

Hi, i have a logfile of a mail server it looks like this: d k 1004210238.380677500 1004210238.454490500 1004210238.679567500 3621 <VOLENDAM@Malawi.com> local.EDAM@Frankrijk.com 6053 81 I have got the following script to set the time in normal gmt time and to filter de failed messages, but i... (2 Replies)
Discussion started by: Prince2
2 Replies
Login or Register to Ask a Question