I have a list of timestamped filenames in this following format:

cat files
  • ..201807010112.log
  • ..201807020112.log
  • ..201807022359.log
  • ..201807030112.log
  • ..201807010412.log

I need to get a certain range and in order to do this, I'd like to use SED.

sed -n '/201807010112/,/201807030112/p' 

Was my first attempt but sed treats the hour and minute strangely, and to make a long story short it's unable to understand this format correctly.

I may be getting in the weeds but in order to solve this, I've decided to convert the format to something SED understands.

cat files | sed -e "s/\([0-9]\{12\}\}/$(date -f '%Y%m%d%H%M' \1)/g"

My problem is that I cannot use the result of the match \1 in the date conversion block.

Is there a way to do this or better yet a better way to get the range of dates?

  • 3
    This is a very good example of a XY question... – don_crissti Aug 6 '18 at 22:23
  • cat 2018070{[1-2],301} – user1133275 Aug 6 '18 at 22:49
  • 1
    like... sycamore? – mikeserv Aug 7 '18 at 0:52
  • 1
    Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community. – Proximo Aug 7 '18 at 1:47
  • Where is your files contents coming from? There might be a better way to get the filenames than from that file, e.g. using find against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem. – Kusalananda Aug 7 '18 at 6:14

Your question states that you need to get the names of some log files in a certain date range.

Let's disregard the fact that you have the filenames in a text file and instead assume that you have direct access to the files in some directory $logdir.

The format of the filenames are *_YYYYMMDDhhmmss.log where the end bit is a standard timestamp string.

To get the files between *_201807010112.log and *_201807030112.log in a loop (to do something to each of them), use (in bash),


for pathname in "$logdir"/*_??????????????.log
    if [ "$process_flag" -eq 0 ]; then
        if [[ "$pathname" == *_201807010112.log ]]; then

    # Do some sort of processing of
    # the logfile in "$pathname" here.

    # When done...

    if [[ "$pathname" == *_201807030112.log ]]; then

This loop loops over all logfiles that have a similar filename format. The loop will iterate over the pathnames in lexicographical order. It is assumed that all files have the same filename prefix (you say nothing about this).

The first part of the loop detects the first file in the range and sets process_flag to 1 when that file is found. Setting process_flag to 1 enables the loop to get into the middle bit where you actually use "$pathname" for whatever processing of that file that you need doing.

Before continuing with the next iteration, the last if statement checks to see whether the current $pathname matches the last file that we wanted to process. If it does, the loop ends by a break statement.


So I decided not to try to use date function to transform the form and instead use more SED!

cat files

  • ..._201807010112.log

  • ..._201807010132.log

  • ..._201807010152.log

  • ..._201807010202.log

The date is in the format YYYYmmddHHSS and I transformed it to the following format:

YYYY-mm-dd HH:SS using this regex expression with SED.

cat files | sed -e 's/\(_[0-9]\{4\}\)\([0-9]\{2\}\) \
\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\) \/\1-\2-\3 \4:\5/g'

Next, from there it was a piece of cake to specify the range based on date.

  | sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'

The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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