perl awk OR sed, search between two timestamps

后端 未结 5 959
北荒
北荒 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: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 () {
      my $time = (split /-/)[1];
      print if $time ge '19:15' and $time le '19:20';
    }
    
    __DATA__
    2014/05/08-19:15:44.544824-
    2014/05/08-19:21:54.544824-
    2014/05/08-19:34:59.564461-
    

    output

    2014/05/08-19:15:44.544824-
    

提交回复
热议问题