perl awk OR sed, search between two timestamps

后端 未结 5 961
北荒
北荒 2021-01-24 20:44

I have a file with following sample text. (The actual text is huge).

2014/05/08-19:15:44.544824-
2014/05/08-19:21:54.544824-
2014/0         


        
相关标签:
5条回答
  • 2021-01-24 21:10

    Using a perl one-liner. Capture the time and then just compare.

    perl -ne '$t = /(\d+:[\d:.]+)/ ? $1 : undef; 
        print if $t ge "19:15:00" && $t le "19:20:00";' file.txt
    
    0 讨论(0)
  • 2021-01-24 21:12

    Using awk

    awk '{gsub(/:/,X,$2)}$2>=191500&&$2<=192000' FS="[-.]" file
    
    0 讨论(0)
  • 2021-01-24 21:14

    It may not be obvious, but a date/time representation that has fixed-width fields in decreasing order of magnitude (like ISO 8601 %Y-%m-%dT%H:%M:%S) can simply be compared as strings, so '19:21:54.544824' gt '19:20' is true, while 19:15:44.544824 lt '19:15' is false.

    That means you can just use split to extract the field and do literal comparisons, like this

    use strict;
    use warnings;
    
    while (<DATA>) {
      my $time = (split /-/)[1];
      print if $time ge '19:15' and $time le '19:20';
    }
    
    __DATA__
    2014/05/08-19:15:44.544824-<String1>
    2014/05/08-19:21:54.544824-<String2>
    2014/05/08-19:34:59.564461-<String3>
    

    output

    2014/05/08-19:15:44.544824-<String1>
    
    0 讨论(0)
  • 2021-01-24 21:21

    The awk and sed commands you show above won't work because they're doing pattern matching -- they're not comparing timestamps to each other in chronological terms (ie, if it doesn't see an exact 19:20:00 string, it will continue to the end even if it sees a 19:21:00 along the way).

    You could probably do it in perl using something similar to the two lines you show at the end where you're generating the timestamps, but in reverse to parse them, convert them to a time object and compare their values.

    0 讨论(0)
  • 2021-01-24 21:32

    Why all the complexity?

    $ awk -F'[-.]' '"19:15:00"<=$2 && $2<="19:20:00"' file
    2014/05/08-19:15:44.544824-<String1>
    

    or less readably but more efficiently if the file is sorted:

    $ awk -F'[-.]' '$2>"19:20:00"{exit} $2>="19:15:00"' file
    2014/05/08-19:15:44.544824-<String1>
    
    0 讨论(0)
提交回复
热议问题