Searching by date range from filenames


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Searching by date range from filenames
# 1  
Old 08-26-2010
Searching by date range from filenames

Hello all,

i have tons of files in folder named like this (yyyymmdd):

bookcollection20100729
bookcollection20100730
bookcollection20100731
bookcollection20100801
bookcollection20100802

etc.

I need to find files with date range in there names lets say from 2010.07.30 - 2010.08.02

I tried with grep

grep -h --mmap 'horror' /usr/local/data/books/bookcollection201007[10-11]*

But it doesn't work, i can change just date, and not year and months.

The plan is that I will have a script that will ask from user date range input as 2010.07.30 - 2010.08.02 then I will add those years, months and days to variables that I can append to grep/sed command and display result

But I m stuck. Any ideas?

Last edited by Whit3H0rse; 08-26-2010 at 10:28 PM.. Reason: error
# 2  
Old 08-26-2010
Code:
ls bookcollection* |awk -v start=20100730 -v end=20100802 '{d=substr($0,15); if (d>=start&&d<=end) print}'

or

Code:
D1="20100730"
D2="20100802"

ls bookcollection* |awk -v start=$D1 -v end=$D2 '{d=substr($0,15); if (d>=start&&d<=end) print}'


Last edited by rdcwayx; 09-06-2010 at 03:47 AM..
# 3  
Old 08-27-2010
This doesn't work, I forgot to mention that there si time append so filenames are bookcollection20100728_001500.

Time doesn't matter to me, just yy, mm and dd. Any new suggestion or reformulation?

Thanks in advance

Regards

==========EDIT
Tx a alot it works, I new at awk so I needed more time to figure it out Smilie

Last edited by Whit3H0rse; 08-28-2010 at 12:19 PM..
# 4  
Old 08-29-2010
Oke, this works great but how to include the "start" and "end" variables into results. Because when i enter range for today 2010.08.29 - 2010.08.29, it display nothing.

Now, it displays this for rage 2010.07.30 - 2010.08.02:
bookcollection20100731
bookcollection20100801

But I need that awk displays this:
bookcollection20100730
bookcollection20100731
bookcollection20100801
bookcollection20100802

Tx,

Regars
# 5  
Old 08-29-2010
Do you mean something like this?

Code:
D1="20100730"
D2="20100802"
grep -h 'horror' $( ls -d bookcollection* | sed -n "/$D1/,/$D2/p" )

This is assuming that your ls sorts alphabetically, otherwise you could perhaps use:
Code:
grep -h 'horror' $( ls -d bookcollection* | sort | sed -n "/$D1/,/$D2/p" )

# 6  
Old 08-29-2010
Code:
ls bookcollection* |awk -F \_ -v start=20100730 -v end=20100802 '{d=substr($1,15); if (d>=start&&d<=end) print}'

# 7  
Old 09-05-2010
Ty people, this works great Smilie

---------- Post updated at 08:42 AM ---------- Previous update was at 03:05 AM ----------

Quote:
Originally Posted by rdcwayx
Code:
ls bookcollection* |awk -F \_ -v start=20100730 -v end=20100802 '{d=substr($1,15); if (d>=start&&d<=end) print}'

SED version works but it is slow because of | sort|, so I prefer AWK, and this code above works great except it doesn't display last day =20100802 it stops on 20100801, also when I choose for example start=20100808
and end=20100808 it display nothing???

I tried modifying code buy my AWK skills are terrible:/, any help?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt 2012-06-15 08:13" ! -newermt 2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

2. Shell Programming and Scripting

Searching range of filenames witn and without numbers on the end

So I'm grepping for the following right now: ls -la /somedirectory/*.log* | awk '{print $9}' The problem with this is that I get the following output: /somedirectory/errors_1_foo.log /somedirectory/errors_1_foo.log.1 /somedirectory/errors_1_foo.log.2... (4 Replies)
Discussion started by: LinuxRacr
4 Replies

3. Shell Programming and Scripting

Searching same filenames in different partitions

Hi, We are doing some migration work. The same files may exist in different partition/mountpoints. We may place it in a single place, hence we don't want any duplicates...we will rename the file names if we know the files which has duplicates(by file name)). It would be nice if a script... (3 Replies)
Discussion started by: saravanapandi
3 Replies

4. Shell Programming and Scripting

Searching for unknown date inside the file and replace to new date

Hello, Iam a newbies to Shell scripting. Iam trying to replace the date inside the file to new date. is there anyway that we can just use the pattern to search as "..." I have many files want to replace with the same date, and each file contains different date. Thanks for your help. ... (2 Replies)
Discussion started by: Daro
2 Replies

5. UNIX for Dummies Questions & Answers

Searching for a pattern from filenames stored in a file

Hi, I have got some 10 filenames stored in a file or displayed in the console as a result of some query i made.. Now I need to open each of these files and search for a pattern in these 10 files.. Can someone help me with this? Thanks, Jean (9 Replies)
Discussion started by: jeanjkj
9 Replies

6. UNIX for Dummies Questions & Answers

Need help in searching a file by range

hi, i need to search a file by range, the file (f3.txt) contains: x1=0123318739 x2=0120123456 x3=0120453576 x4=0110445654 x5=0120432343 x6=0129423 x7=0104323433 x8=01232132134 x9=0122344242 x10=012006196 x11=012016546 x12=012091235 x13=0121064598 x14=012194562 x15=0122028556... (3 Replies)
Discussion started by: takyeldin
3 Replies

7. Shell Programming and Scripting

Searching the lines within a range of time period in a text file

Dear All, Please advice me, I have a text file with one field date and time like below given. I need to find out the lines whchi content the time stamp between Wed May 26 11:03:11 2010 and Wed May 26 11:03:52 2010 both can be included, using awk command which could be an interactive so that I... (6 Replies)
Discussion started by: chinmayadalai
6 Replies

8. UNIX for Dummies Questions & Answers

Searching for specific filenames

Hi I would like to know how to search through a directory and pull out files that has a specific pattern in the filename. For example if the filename has "bsc" in it, then that file must be moved to another directory where I will perform some operations on it. I know grep can be used, but I'm... (17 Replies)
Discussion started by: ladyAnne
17 Replies

9. UNIX for Advanced & Expert Users

Searching filenames containing certain text???

Suppose there are multiple files containing certain text "abc". How to print the name of all such files with a single command from unix prompt? Thanks in advance (6 Replies)
Discussion started by: skyineyes
6 Replies

10. Shell Programming and Scripting

searching for filenames with search strings in another file

Hi, I have 5 files in a directory. emp1_usage.txt emp2_usage.txt emp3_usage.txt emp4_usage.txt emp5_usage.txt I am using sqlldr to get the contents of the above 5 files and store it in a temp table and update my original table using temp table. for f in *emp*.txt do sqlldr... (3 Replies)
Discussion started by: pathanjalireddy
3 Replies
Login or Register to Ask a Question

Featured Tech Videos