Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a directory with files listed like so:

abc201303130110.txt
abc201303140511.txt
abc201303150112.txt

As you can see, the filename contains a datetime stamp. I want to return all files with the value 201303140000 or higher in the filename.

I can get use grep to grab the datetime portion of the filename, but I don't know how to pipe through a filter that can do something like X > 201303140000.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

If your awk is the GNU awk, you can simply use:

find . -type f | awk '{ match($0, /2013[0-9]+/,arr)}  arr[0] >= 201303140000 '
share|improve this answer
    
I see what you're doing here. You're pulling all dates within filenames, grabbing the ones within my criteria, then for each remaining date you run a grep. This seems highly inefficient as I need to process thousands of files/filenames often. –  Chaos Mar 24 '13 at 8:49
    
@Chaos I have updated final solution –  Rahul Patil Mar 24 '13 at 10:34
    
Does GNU awk support 64-bit integers everywhere, even if the local C compiler is an old one without long long? –  Gilles Mar 24 '13 at 15:41
1  
@Gilles, awk uses doubles not integers. In this case though, you can force string comparison (arr[0] >= "201303140000") to avoid that kind of problem. –  Stephane Chazelas Mar 24 '13 at 16:35
    
Thanks Rahul, I changed the regex a little, but your solution works great! –  Chaos Mar 26 '13 at 6:20
add comment

Perl to the rescue! Perl's -n switch works a bit like sed -n and perl is more powerful in its matching and arithmetic.

dennis@lightning:/tmp/foo$ ls -1
abc201303130110.txt
abc201303140511.txt
abc201303150112.txt
dennis@lightning:/tmp/foo$ ls | perl -ne '$d=$_; $d =~ s/.*?(\d+).*/\1/s; print if $d >= 201303140000'
abc201303140511.txt
abc201303150112.txt
share|improve this answer
    
Why down vote this answer? –  warl0ck Mar 24 '13 at 11:36
1  
You can simplify that to perl -ne 'print if /\d+/ && $& >= 201303140000' –  Stephane Chazelas Mar 24 '13 at 12:11
    
I tried this, and it works of course. Thanks for the suggestion –  Chaos Mar 26 '13 at 6:22
add comment

With zsh:

setopt extendedglob # best in ~/.zshrc

print -rl abc<201303140000->.txt

<x-y> is a zsh globbing operator only available with extendedglob as it potentially conflicts with redirection operators.

share|improve this answer
    
Does zsh use 64-bit integers everywhere, even 32-bit systems whose C compiler is C89 and doesn't support long long? –  Gilles Mar 24 '13 at 15:41
    
This is really convenient, thanks for the suggestion. –  Chaos Mar 26 '13 at 6:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.